图像中的 CGPath
我需要根据图像(任何可能的形状)创建蒙版。我需要:
- 使用 CGPath 创建 CALayer 作为适当的掩码
- 调用 [UIView.layer setMask: with layer above ]
所以主要的大问题是如何根据黑白图像设置 CGPath?或者是否可以直接从图像放置遮罩?
谢谢!
面具的示例图像:
I need to create mask based on image (any possible shape). I need to:
- Create CALayer with CGPath as a proper mask
- Call [UIView.layer setMask: with layer above ]
So main big question is how to set CGPath based on black-white image? Or is it possible to place mask directly from image?
Thanks!
Example image of a mask:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要创建带有 Alpha 通道的图像。根据 CALayer 的 mask 属性文档:
因此,您想要用作遮罩的图层需要一个 Alpha 通道。如果你想使用图像来创建这样的通道,图像本身需要有一个 Alpha 通道。因此,您需要将上面发布的黑白蒙版复制到图像的 Alpha 通道中。该特定图像的颜色将被忽略。然后加载图像,为其设置图层内容并将该图层用作不同的图层蒙版。与此相关的事情:
在浏览器中编码,请原谅系统错误。
将所有蒙版存储为 png 文件可能会增大应用程序的大小。您可以将蒙版存储为 jpg,并在运行时使用 CoreGraphics 绘图函数创建适当的 alpha 版本。但这是另一个问题了......
You will need to create your image with an alpha channel. According to
CALayer
's mask property documentation:So the layer you want to use as mask needs an alpha channel. If you want to use an image to create such a channel, the image itself needs to have an alpha channel. So you would need to copy the black and white mask you posted above into the alpha channel of an image. The color of that particular image is ignored. You then load the image, set a layers contents to it and use that layer as a different layers mask. Something around this:
Coded in the browser, excuse systactical errors.
Storing all your masks as png files might blow your app size. You could store the masks as jpgs and create the appropriate alpha version at runtime using the CoreGraphics drawing functions. But that is another question.....
我认为与我不久前所做的事情非常相似。基本上,您需要从 Adobe Illustrator 导出文件,然后运行例程将该文件转换为 CGPath。
您将在我对这个问题的回答中找到如何做到这一点:
如何检测/处理弯曲区域上的触摸事件?
Very similar issue I think to something I did a while back. Basically, you'll need to export the file from Adobe Illustrator then run a routine to convert that file into a CGPath.
You'll find how to do it in my answer to this question:
How to detect/handle touch events on curved regions?