如何在iOS上擦除UIImageView图像的某些部分? [英] How to erase some portion of a UIImageView's image on iOS?

查看:345
本文介绍了如何在iOS上擦除UIImageView图像的某些部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIImageView的视图,并设置了一个图像。我想删除图像,就像我们在带有橡皮擦的Photoshop中一样。我该如何实现这一目标?另外,如何撤消擦除?

I have a view with UIImageView and an image set to it. I want to erase the image as something like we do in photoshop with an eraser. How do I achieve this? Also, how do I undo erase?

推荐答案

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

If you know what area you want to erase, you can create a new image of the same size, set the mask to the full image minus the area you want to erase, draw the full image into the new image, and use that as the new image. To undo, simply use the previous image.

修改

示例代码。假设要从图像视图中删除的区域 imgView erasePath 指定:

Sample code. Say the area you want to erase from image view imgView is specified with by 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();
}

这篇关于如何在iOS上擦除UIImageView图像的某些部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆