【发布时间】:2010-12-02 00:52:53
【问题描述】:
我有一个带有 UIImageView 的视图和一个为其设置的图像。我想像在 Photoshop 中用橡皮擦一样擦除图像。我如何实现这一目标?另外,如何撤消擦除?
【问题讨论】:
标签: objective-c iphone core-graphics erase
我有一个带有 UIImageView 的视图和一个为其设置的图像。我想像在 Photoshop 中用橡皮擦一样擦除图像。我如何实现这一目标?另外,如何撤消擦除?
【问题讨论】:
标签: objective-c iphone core-graphics erase
如果您知道要擦除的区域,则可以创建一个相同大小的新图像,将遮罩设置为整个图像减去您要擦除的区域,将整个图像绘制到新图像中,然后使用那作为新的形象。要撤消,只需使用上一张图片即可。
编辑
示例代码。假设您要从图像视图imgView 中删除的区域由erasePath 指定:
- (void) clipImage
{
UIImage *img = imgView.image;
CGSize s = img.size;
UIGraphicsBeginImageContext(s);
CGContextRef g = UIGraphicsGetCurrentContext();
CGContextAddPath(g,erasePath);
CGContextAddRect(g,CGRectMake(0,0,s.width,s.height));
CGContextEOClip(g);
[img drawAtPoint:CGPointZero];
imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
【讨论】:
UIGraphicsBeginImageContext(imgBlankView.frame.size);
[imgBlankView.image drawInRect:CGRectMake(0, 0, imgBlankView.frame.size.width, imgBlankView.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(),lineWidth);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint1.x, lastPoint1.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
imgBlankView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
【讨论】:
Swift 5.2 版本
UIGraphicsBeginImageContextWithOptions(imageView.frame.size, false, 1.0)
let currentContext = UIGraphicsGetCurrentContext()
currentContext?.addPath(shapeLayer.path!)
currentContext?.addRect(CGRect(x: 0, y: 0, width: imageView.frame.size.width, height: imageView.frame.size.height))
currentContext?.clip(using: .evenOdd)
imageView.image?.draw(at: .zero)
let imageTeemp = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext()
【讨论】: