如何在iOS上擦除UIImageView图像的某些部分?

Rah*_*yas 1 iphone core-graphics objective-c erase

我有一个UIImageView视图和一个图像设置.我想删除图像就像我们在带有橡皮擦的photoshop中所做的那样.我该如何实现这一目标?另外,如何撤消擦除?

Ed *_*rty 7

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

编辑

示例代码.假设您要从图像视图中删除的区域使用以下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();
}
Run Code Online (Sandbox Code Playgroud)

  • 嗨@ RahulVyas,@ Dhaval,@ Ed Marty你能告诉我你如何获得ErasePath,如果你有... (2认同)