开发者

Fatsest way to edit alpha of CGImage (or UIImage) with touch and then display?

I have two image views, one on top of the another, with two different images. As the user t开发者_高级运维ouches 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜