从 CGImage 中修剪 alpha
我需要得到一个修剪过的 CGImage。我有一个图像,在某些颜色周围有空白空间(alpha = 0),需要修剪它以获得仅可见颜色的大小。
谢谢。
I need to get a trimmed CGImage. I have an image which has empty space (alpha = 0) around some colors and need to trim it to get the size of only the visible colors.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有三种方法可以做到这一点:
1)使用 Photoshop(或选择的图像编辑器)来编辑图像 - 我认为你不能这样做,这太明显了!
2)忽略它 - 为什么不直接忽略它,绘制完整尺寸的图像?它是透明的,因此用户永远不会注意到。
3) 编写一些代码来遍历图像中的每个像素,直到到达 alpha 值 > 的像素。 0。这应该为您提供从顶部修剪的行数。但是,这会减慢您的 UI,因此您可能希望在后台线程上执行此操作。
例如,
当您知道图像周围的透明空间量时,您可以使用 UIImageView 绘制它,将 renderMode 设置为居中并让它为您进行修剪:)
There's three ways of doing this :
1) Use photoshop (or image editor of choice) to edit the image - I assume you can't do this, it's too obvious an answer!
2) Ignore it - why not just ignore it, draw the image it's full size? It's transparent so the user will never notice.
3) Write some code that goes through each pixel in the image until it gets to one that has an alpha value > 0. This should give you the number of rows to trim from the top. However, this will slow down your UI so you might want to do it on a background thread.
e.g.
When you know the amount of transparent space from around the image you can draw it using a UIImageView, set the renderMode to center and let it do the trimming for you :)