通过触摸编辑 CGImage(或 UIImage)的 alpha 然后显示的最简单方法?
我有两个图像视图,一个在另一个之上,有两个不同的图像。当用户触摸图像并移动他/她的手指时,顶部图像应该沿着具有固定半径的触摸点变得透明。 (如 PhotoChop 应用程序)。
目前我正在这样做......
- 每次触摸。
- 从顶部图像的 CGImage 获取图像缓冲区的副本。
- 编辑缓冲区的 Alpha 通道以创建以触摸点为中心的透明圆圈。
- 从缓冲区创建新的 CGImage。
- 从 CGImage 创建 UIImage 并使用新的 UIImage 作为顶部图像视图的图像。
这可行,但正如您所看到的,涉及太多复制、创建,而且速度很慢。
有人可以建议我一种更快的方法来做同样的事情吗?
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...
- For each touch.
- Get a copy of the image buffer from CGImage of the top image.
- Edit the alpha channel of the buffer to create a transparent circle centered at the touch point.
- Create new CGImage from the buffer.
- 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?
当用户绘制时,根据更改修改 CGBitmapContext。保留引用上下文的链接
CGImage
。根据原始图像和蒙版创建一个蒙版图像,并从中创建一个UIImage
。CGImage
以及CGBitmapContext
必须是灰度的。您可以使用CGImageMaskCreate
或CGImageCreate
来制作图像,但首选前者。我不确定其内部结构,但可能每次绘制
UIImage
时,它都会一直引用回CGBitmapContext
。换句话说,我认为不会复制任何内容,您所需要的只是包含 UIImage 的视图上的setNeedsDisplay
。As the user draws, modify a
CGBitmapContext
with the changes. Keep a linkedCGImage
that refers to the context. Create a masked image from the original and the mask, and create aUIImage
from that.The
CGImage
, and thus theCGBitmapContext
, must be grayscale. You can use eitherCGImageMaskCreate
orCGImageCreate
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 theCGBitmapContext
. In other words, I do not think anything is copied and all you will need is asetNeedsDisplay
on the view containing the UIImage.