Objective c 如何擦除UIImageView的某些部分';iOS上的图像是什么?

Objective c 如何擦除UIImageView的某些部分';iOS上的图像是什么?,objective-c,iphone,core-graphics,erase,Objective C,Iphone,Core Graphics,Erase,我有一个带有UIImageView的视图,并为其设置了一个图像。我想像在photoshop中用橡皮擦一样擦除图像。我如何做到这一点?另外,如何撤消擦除?如果您知道要擦除的区域,可以创建相同大小的新图像,将遮罩设置为完整图像减去要擦除的区域,将完整图像绘制到新图像中,并将其用作新图像。要撤消,只需使用上一个图像 编辑 示例代码。假设要从图像视图中擦除的区域由erasePath指定: - (void) clipImage { UIImage *img = imgView.image;

我有一个带有UIImageView的视图,并为其设置了一个图像。我想像在photoshop中用橡皮擦一样擦除图像。我如何做到这一点?另外,如何撤消擦除?

如果您知道要擦除的区域,可以创建相同大小的新图像,将遮罩设置为完整图像减去要擦除的区域,将完整图像绘制到新图像中,并将其用作新图像。要撤消,只需使用上一个图像

编辑

示例代码。假设要从图像视图中擦除的区域由
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();
}
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()

Thanx表示示例代码,但使用CGContextAddPath(g,橡皮擦路径);如何获得擦除路径?请帮帮我。嗨@RahulVyas,@Dhaval,@Ed Marty,你能告诉我你是如何得到橡皮擦路径的吗,如果你有..这里是如何得到“橡皮擦路径”的。只需将要裁剪为“cropRect”的选定CGRect传递给CGMutablePathRef erasePath=CGPathCreateMutable();CGPathAddRect(擦除路径,NULL,cropRect);CGContextAddPath(g,擦除路径);
        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()