стереть рисунок, сохраненный в другом представлении изображения Xcode

Я использую следующий код для рисования изображения с помощью UITouch. Это отлично работает, и я сохраняю изображение из временного изображения в другое представление изображения по ходу дела. Проблема в том, что я хочу вернуться и стереть часть рисунка из сохраненного изображения. Я знаю, что могу установить белый цвет, и он будет казаться стертым, но я рисую поверх другого изображения, содержащего изображение из моей фотопленки. поэтому мне нужно полностью очистить сохраненный рисунок.

- (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]

//здесь я пытаюсь создать стирание, но все, что у меня получается, это черная линия

    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();



}

person user1114881    schedule 29.11.2014    source источник


Ответы (2)


Я понял это. Я поместил оператор if для стирания «clear2» в начале -(void) draw2{, переписав его, сделав свой рисунок на imageView и установив blendMode как kCGBlendModeCopy. Затем я просто помещаю все остальное в оператор else, и все работает.
Вот код.

- (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();
}


}
person user1114881    schedule 30.11.2014

вы можете вызывать этот метод где угодно.

- (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];

}
person IOS developer    schedule 17.07.2015