erase drawing that is saved to another image view Xcode

166 views Asked by At

I am using the following code to draw on an image with UITouch. This works fine and I save the image from a temporary imageView to another image view as I go. The problem is that I want to go back in and erase some of the drawing from the saved imageView. I know that I can set the color to white and it will appear to erase it but I am drawing on top of another imageView that contains an image from my camera roll. so I need to completely clear the saved drawing.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{



    self.RealImage.frame = CGRectMake(14-(ax2*subtract),138-(ay2*subtract),740*see,850*see);

    self.imageView.frame = CGRectMake(14-(ax2*subtract),138-(ay2*subtract),740*see,850*see);

    self.tempImage.frame = CGRectMake(14-(ax2*subtract),138-(ay2*subtract),740*see,850*see);



    ctr = 0;
    UITouch *touch = [touches anyObject];
    pts[0] = [touch locationInView:self.tempImage];

    lastPoint = [touch locationInView:tempImage];
}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{


    self.RealImage.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);

    self.imageView.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);

    self.tempImage.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);


    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self.tempImage];
    currentPoint = [touch locationInView:tempImage];
    ctr++;
    pts[ctr] = p;

    if (ctr == 4)
    {
        pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); // move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of the second Bezier segment
        [path moveToPoint:pts[0]];
        [path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]]; // add a cubic Bezier from pt[0] to pt[3], with control points pt[1] and pt[2]

        [self draw2];


        // replace points and get ready to handle the next segment
        pts[0] = pts[3];
        pts[1] = pts[4];
        ctr = 1;
    }
     NSLog(@"ctr:%d",ctr);
    lastPoint = currentPoint;
}



- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{

    king  = pencilString;

    [path removeAllPoints];
    ctr = 0;



    UIGraphicsBeginImageContext(self.tempImage.frame.size);
    self.RealImage.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);

    self.imageView.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);

    self.tempImage.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);
    [self.imageView.image drawInRect:CGRectMake(0,0, self.imageView.frame.size.width, self.imageView.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
    [self.tempImage.image drawInRect:CGRectMake(0,0, self.tempImage.frame.size.width, self.tempImage.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];

    self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    self.tempImage.image = nil;
    if (see ==2) {


    self.RealImage.frame = CGRectMake(14-(self.zoom),138-(self.first),1480,1700);

    self.imageView.frame = CGRectMake(14-(self.zoom),138-(self.first),1480,1700);

        self.tempImage.frame = CGRectMake(14-(self.zoom),138-(self.first),1480,1700);}


    UIGraphicsEndImageContext();



}

- (void)draw2
{



    self.RealImage.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);

    self.imageView.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);

    self.tempImage.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);
    UIGraphicsBeginImageContext(self.tempImage.frame.size);
    [self.tempImage.image drawInRect:CGRectMake(0, 0, self.tempImage.frame.size.width, self.tempImage.frame.size.height)];

    pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); // move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of the second Bezier segment
    [path moveToPoint:pts[0]];
    [path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]]; // add a cubic Bezier from pt[0] to pt[3], with control points pt[1] and pt[2]

//here's where I try to create the erasure but all I get it a black line

    if ([pencilString isEqualToString:@"clear2"]) {

        mode = DrawingModePen;
        if (mode == DrawingModePen) {
            NSLog(@"drawing");
            CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor clearColor
                                                                              ] CGColor]);
        }

        UIGraphicsBeginImageContext(self.imageView.frame.size);



        [self.imageView.image drawInRect:CGRectMake(0, 0, self.imageView.frame.size.width, self.imageView.frame.size.height)];
       CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);

    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);

        self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
        [self.imageView setAlpha:1.0];

        [path stroke];

        CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeCopy);
           }
    else if ([pencilString isEqualToString:@"red"]) {
        [[UIColor colorWithRed:255.0/255.0 green:0.0 blue:0.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"blue"]) {
        [[UIColor colorWithRed:51.0/255.0 green:76.0/255.0 blue:255.0/255.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"yellow"]) {
        [[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:0.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"orange"]) {
        [[UIColor colorWithRed:255.0/255.0 green:90.0/255.0 blue:0.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"green"]) {
        [[UIColor colorWithRed:0.0 green:204.0/255.0 blue:0.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"purple"]) {
        [[UIColor colorWithRed:153.0/255.0 green:0.0 blue:255.0/255.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"redOrange"]) {
        [[UIColor colorWithRed:255.0/255.0 green:51.0/255.0 blue:0.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"yellowOrange"]) {
        [[UIColor colorWithRed:255.0/255.0 green:153.0/255.0 blue:0.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"blueViolet"]) {
        [[UIColor colorWithRed:102.0/255.0 green:51.0/255.0 blue:255.0/255.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"blueGreen"]) {
        [[UIColor colorWithRed:0.0 green:153.0/255.0 blue:204.0/255.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"redViolet"]) {
        [[UIColor colorWithRed:255.0/255.0 green:0.0 blue:153.0/255.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"yellowGreen"]) {
        [[UIColor colorWithRed:204.0/255.0 green:255.0/255.0 blue:0.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"white"]) {
        [[UIColor whiteColor] setStroke];
    }

    else if ([pencilString isEqualToString:@"ltGray"]) {
        [[UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"dkGray"]) {
        [[UIColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102.0/255.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"brown"]) {
        [[UIColor colorWithRed:102.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"tan"]) {
        [[UIColor colorWithRed:255.0/255.0 green:204.0/255.0 blue:102.0/255.0 alpha:1.0] setStroke];
    }

    else if ([pencilString isEqualToString:@"black"]) {
        [[UIColor blackColor] setStroke];
    }

    [path stroke];

    CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);

    //    CGContextStrokePath(UIGraphicsGetCurrentContext());
    self.tempImage.image = UIGraphicsGetImageFromCurrentImageContext();
    [self.tempImage setAlpha:1.0];
    self.RealImage.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);

    self.imageView.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);

    self.tempImage.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);

    UIGraphicsEndImageContext();



}
2

There are 2 answers

0
user1114881 On

I figured this out. I placed the if statement for the "clear2" erasure at the beginning of -(void) draw2{ rewriting it by making my drawing on the imageView and having the blendMode be kCGBlendModeCopy. Then I just put everything else inside the else statement and it works.
Here is the code.

- (void)draw2
{

if ([pencilString isEqualToString:@"clear2"]) {

    UIGraphicsBeginImageContext(imageView.frame.size);
    [[UIColor clearColor] setStroke];
    [imageView.image drawInRect:CGRectMake(0,0,imageView.frame.size.width, imageView.frame.size.height)];




    CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeCopy);



    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);

    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);

    CGContextStrokePath(UIGraphicsGetCurrentContext());

     [path setLineWidth:20.0];


        [path stroke];


imageView.image = UIGraphicsGetImageFromCurrentImageContext();



}
else {

UIGraphicsBeginImageContext(self.tempImage.frame.size);
[self.tempImage.image drawInRect:CGRectMake(0, 0, self.tempImage.frame.size.width, self.tempImage.frame.size.height)];

pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); // move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of the second Bezier segment
[path moveToPoint:pts[0]];
[path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]]; // add a cubic Bezier from pt[0] to pt[3], with control points pt[1] and pt[2]




if ([pencilString isEqualToString:@"red"]) {
    [[UIColor colorWithRed:255.0/255.0 green:0.0 blue:0.0 alpha:1.0] setStroke];
    [path setLineWidth:10.0*see];
}
[path stroke];

CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);

//    CGContextStrokePath(UIGraphicsGetCurrentContext());
self.tempImage.image = UIGraphicsGetImageFromCurrentImageContext();
[self.tempImage setAlpha:1.0];


UIGraphicsEndImageContext();
}


}
0
IOS developer On

you can call this method anywhere.

- (void)eraseLine
{

    UIGraphicsBeginImageContext(self.bounds.size);

    [self.viewImage drawInRect:self.bounds];

    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);

    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), lineWidth);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), previousPoint.x, previousPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);

    CGContextStrokePath(UIGraphicsGetCurrentContext());
    self.viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    previousPoint = currentPoint;

    [self setNeedsDisplay];

}