Rahul Vyas Rahul Vyas - 1 year ago 82
Objective-C Question

How to erase some portion of a UIImageView's image on iOS?

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?

Answer Source

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.


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;
  CGContextRef g = UIGraphicsGetCurrentContext();
  [img drawAtPoint:CGPointZero];
  imageView.image = UIGraphicsGetImageFromCurrentImageContext();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download