通过触摸编辑 CGImage(或 UIImage)的 alpha 然后显示的最简单方法?

发布于 2024-08-31 16:00:27 字数 377 浏览 2 评论 0原文

我有两个图像视图,一个在另一个之上,有两个不同的图像。当用户触摸图像并移动他/她的手指时,顶部图像应该沿着具有固定半径的触摸点变得透明。 (如 PhotoChop 应用程序)。

目前我正在这样做......

  1. 每次触摸。
  2. 从顶部图像的 CGImage 获取图像缓冲区的副本。
  3. 编辑缓冲区的 Alpha 通道以创建以触摸点为中心的透明圆圈。
  4. 从缓冲区创建新的 CGImage。
  5. 从 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...

  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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深空失忆 2024-09-07 16:00:27
CGImageCreateWithMask

当用户绘制时,根据更改修改 CGBitmapContext。保留引用上下文的链接CGImage。根据原始图像和蒙版创建一个蒙版图像,并从中创建一个 UIImage

CGImage 以及 CGBitmapContext 必须是灰度的。您可以使用 CGImageMaskCreateCGImageCreate 来制作图像,但首选前者。

我不确定其内部结构,但可能每次绘制 UIImage 时,它都会一直引用回 CGBitmapContext。换句话说,我认为不会复制任何内容,您所需要的只是包含 UIImage 的视图上的 setNeedsDisplay

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文