清除UIImageView触摸绘制的内容

时间:2012-06-26 20:17:53

标签: ios ios5 uiimageview

我的类Canvas继承自 UIImageView (在背景是set image background.png),并且在方法touchesMoved中我绘制了一些颜色的手势:

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, width);
CGContextSetRGBStrokeColor(ctx, red, green, blue, alpha);
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, location.x, location.y);
CGContextAddLineToPoint(ctx, currentLocation.x, currentLocation.y);
CGContextStrokePath(ctx);
self.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

location = currentLocation;

我有ERASE按钮,如何清除画布的上下文但是将background.png保留为背景,只删除用户使用触摸绘制的内容(我有属性画布)?

1 个答案:

答案 0 :(得分:2)

您可以在图像视图上绘制透明矩形,如下所示:

CoreGraphics: Image Transparent Background