屏蔽 UIImage
我正在开发一个可以更改边框或矩形 UIImage 的应用程序。边框会有所不同,但看起来就像 UIImage 是用剪刀剪下来的,或者有其他影响。
最好的方法是什么?
我的第一个想法是准备一堆具有我正在寻找的正确边框效果的透明 PNG,然后以某种方式将其用作我的 UIImage 的蒙版。这是正确的道路吗?或者有更灵活的编程方式来做到这一点?
I'm working on an app that can change the borders or a rectangular UIImage. The borders will vary, but will look like the UIImage was cut out with scissors, or something to that affect.
What is the best way to do this?
My first thought is to prep a bunch of transparent PNGs with the correct border effect I'm looking for, and then somehow use that as a mask for my UIImage. Is this the right path? Or is there a more flexible programmatic way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下是可用于遮罩图像的 Core Graphics 调用:
Here are the Core Graphics calls that you can use to mask the image:
为此,您需要图像屏蔽,我写了一个关于如何使用它的教程以及我如何在自己的应用程序中使用它。
以下示例代码应该可以帮助您入门,它需要原始图像和蒙版图像作为输入,并返回蒙版图像作为输出。
You need image masking for that, I wrote a tutorial on how to use it and how I've used it in my own application.
The following example code should help you get started, it needs the original image and a mask image as input and it returns the masked image as output.
是的,这就是您需要做的。最好的方法是使用 Core Graphics; Quartz 2D 编程指南有一个 描述如何用其他图像遮盖图像的文章。您可以通过
UIImage
对象通过其CGImage
属性获取CGImageRef
(这些 Core Graphics 方法使用的),然后获取UIImage
使用UIImage
类方法+imageWithCGImage:
从更改(屏蔽)的CGImageRef
返回。Yep, that's pretty much what you need to do. The best approach is to use Core Graphics; the Quartz 2D Programming guide has an article describing how to mask images with other images. You can obtain a
CGImageRef
(which these Core Graphics methods use) from aUIImage
object via itsCGImage
property, and then get aUIImage
back from your altered (masked)CGImageRef
using theUIImage
class method+imageWithCGImage:
.还有另一种方法可以通过路径来做到这一点。我已经发布了用圆圈执行此操作的代码,并且可以轻松编辑以适用于任何闭合路径。
如何在椭圆形或椭圆形上裁剪 UIImage圆形?
There is another way of doing this with a path. I've posted code that does it with an circle and could easily be edited to work with any closed path.
How to crop UIImage on oval shape or circle shape?