views:

284

answers:

1

I have two image views, one on top of the another, with two different images. As the user touches the image and moves his/her finger, the top image should become transparent along the touch points with a fixed radius. (Like the PhotoChop app).

Currently I am doing it this way...

  1. For each touch.
  2. Get a copy of the image buffer from CGImage of the top image.
  3. Edit the alpha channel of the buffer to create a transparent circle centered at the touch point.
  4. Create new CGImage from the buffer.
  5. Create UIImage from the CGImage and use the new UIImage as the top image view's image.

This works but as you can see too many copy, creates are involved and it is slow.

Can somebody please suggest me a faster way of doing the same thing?

+1  A: 
CGImageCreateWithMask

As the user draws, modify a CGBitmapContext with the changes. Keep a linked CGImage that refers to the context. Create a masked image from the original and the mask, and create a UIImage from that.

The CGImage, and thus the CGBitmapContext, must be grayscale. You can use either CGImageMaskCreate or CGImageCreate to make the image, but the former is preferred.

I am not sure of the internals, but it may be that every time you draw the UIImage it will refer all the way back to the CGBitmapContext. In other words, I do not think anything is copied and all you will need is a setNeedsDisplay on the view containing the UIImage.

drawnonward