Iphone 如何删除在uiimageview上绘制的线?

Iphone 如何删除在uiimageview上绘制的线?,iphone,objective-c,cocoa-touch,uiimageview,Iphone,Objective C,Cocoa Touch,Uiimageview,我想在uiimageview中删除在touch事件中绘制的线。 我本想打电话给SetNeedsDisplayinToucheSended,但没用 在Canvas.m中,我有: - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; self.location = [touch locationInView:self]; }

我想在uiimageview中删除在touch事件中绘制的线。 我本想打电话给SetNeedsDisplayinToucheSended,但没用

在Canvas.m中,我有:

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

    UITouch *touch = [touches anyObject];
    self.location = [touch locationInView:self];
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint currentLocation = [touch locationInView:self];

    UIGraphicsBeginImageContext(self.frame.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [self.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    CGContextSetLineCap(ctx, kCGLineCapRound);
    CGContextSetLineWidth(ctx, 5.0);
    CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
    CGContextBeginPath(ctx);
    CGContextMoveToPoint(ctx, location.x, location.y);
    CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
    CGContextStrokePath(ctx);
    self.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    location = currentLocation;
}

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

    [self setNeedsDisplay];
}

简单的解决方案-在开始时保存原始图像,当您想要删除行时,只需执行
self.image=savedImage

但是,更好的解决方案(具有更好的性能)是根本不更改图像,而是在
UIImageView
上有一个透明的视图,并在其中绘制。绘图也可以简单得多(例如,使用
CGImageRef
缓冲区,而不是每次通过
UIGraphicsBeginImageContext
创建图像上下文)