ios - 清除 UIImageView 触摸绘制的内容

标签 ios ios5 uiimageview

我有继承自 UIImageView 的类 Canvas(在背景中设置了图像 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 按钮,如何清除 Canvas 的上下文但保留 background.png 作为背景,只删除用户使用触摸绘制的内容(我有属性 Canvas )?

最佳答案

您可以在 ImageView 上绘制一个透明矩形,如下所示:

CoreGraphics: Image Transparent Background

关于ios - 清除 UIImageView 触摸绘制的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11215299/

相关文章:

ios - UICollectionView 的 viewDidLoad 大小不正确

ios - 为什么这些约束在 iPad 9.7"and 12.9"上不能正常工作?

objective-c - iOS 5 - 从异步调用的方法中捕获返回值

swift - 限制边距在 Xcode 上无法正常工作

iphone - 如何从UIImageView中获取显示的图像帧?

ios - 如何在 ios 中从 appdelegate 呈现不同的 View Controller

ios - 使用 PayPal iOS 框架在 iOS 应用程序中捐款

iphone - 如何访问secureTextEntry以便进一步使用?

ios - 未知的音频驱动程序错误

ios - 在 UIStackView 上使用 UIImageView 自动布局 - iOS Swift