iOS 中的 UIImage 或 CGImage API 是否可以实现这种屏蔽
我有一个带有一些文本的 UIImage,并且想应用图案 UIImage 作为遮罩。这可能吗? 据我所知,通过 UILabel,我们可以使用 CAGradientLayer 获得这种渐变。但是如果源是 UIImage 可以这样做吗?
图像可能有一些符号/图片等,而不是常规字符,因此是 UIImage。我还可以根据上下文应用不同的遮罩图案来重复使用图像。
感谢您的帮助。
编辑:感谢您的所有回答。 我了解将渐变应用于文本标签或创建包含文本的图像。 但我的目标是得到这个。--> 单击此处
即我有一个 png,其中有一些绘图,例如具有透明背景的花朵。我想在运行时使用gradient.png 将渐变应用于该图片内的对象,如上图所示。通过掩蔽可以吗?
谢谢
I have an UIImage with some text and would like to apply pattern UIImage as masking. Is this possible ?
I understand that with UILabel we can get this kind of gradient using CAGradientLayer. But can this be done if the source is an UIImage ?
The image may have some symbols/pictures etc other than regular characters and hence UIImage. Also i could reuse the image by applying different masking pattern depending on the context.
Appreciate your help.
EDIT: Thanks for all your answers.
I understand applying the gradient to a text label or creating an image that has text.
But my goal is to get this.--> Click here
i.e. i have a png with some drawing like a flower with transparent background. I want to apply the gradient to the object inside that picture at runtime with a gradient.png as shown in the picture above. Is that possible with masking ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看起来您应该能够使用 CGImageMaskCreate:
要进行更长的讨论,请查看此处的评论线程:
http://iosdevelopertips.com/cocoa/how-to-mask- an-image.html
Looks like you should be able to use CGImageMaskCreate:
For a longer discussion check out the comment thread here:
http://iosdevelopertips.com/cocoa/how-to-mask-an-image.html
是的,确实如此:)
如果您想要严格控制,Jason Whyne 的想法可能会奏效。但我喜欢这个,因为它短了大约 8 行。
Yes, it is :)
If you want heavy control, Jason Whyne's idea might work. But I like this one, because it's about 8 lines shorter.
这是绘制遮盖某些内容的文本图像的另一种方法。它基于
kCGBlendModeSourceIn
混合模式:您在清晰的背景上绘制文本,然后在整个地方绘制填充。Here's just another way to draw an image of text masking something. It's based on
kCGBlendModeSourceIn
blending mode: you draw text on a clear background and then draw the fill all over the place.使用面罩的样品很好并且效果很好,但是你正在泄漏。
CGImageMaskCreate 和 CGImageCreateWithMask 进行分配(遵循“创建”-> 保留规则)
所以你应该释放面具&使用后的图像:
根据 ADC 文档:
...
返回值
Quartz 位图图像蒙版。您负责通过调用 CGImageRelease 来释放该对象。
sample using mask is fine and works well, but You are leaking.
CGImageMaskCreate and CGImageCreateWithMask do allocate (following "create" -> retain rule)
so You should release mask & image after using:
As per ADC docs:
...
Return Value
A Quartz bitmap image mask. You are responsible for releasing this object by calling CGImageRelease.