从 CGImage 中修剪 alpha

发布于 2024-10-16 13:35:26 字数 87 浏览 4 评论 0原文

我需要得到一个修剪过的 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 技术交流群。

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

发布评论

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

评论(1

吹梦到西洲 2024-10-23 13:35:26

有三种方法可以做到这一点:

1)使用 Photoshop(或选择的图像编辑器)来编辑图像 - 我认为你不能这样做,这太明显了!

2)忽略它 - 为什么不直接忽略它,绘制完整尺寸的图像?它是透明的,因此用户永远不会注意到。

3) 编写一些代码来遍历图像中的每个像素,直到到达 alpha 值 > 的像素。 0。这应该为您提供从顶部修剪的行数。但是,这会减慢您的 UI,因此您可能希望在后台线程上执行此操作。

例如,

// To get the number of transparent rows at the top of the image
// Sorry this code is so ugly
uint32 *p = start_of_image;
while ( 0 == *p & 0x000000ff && p < end_of_image_data) ++p;
uint number_of_white_rows_at_top = (p - start_of_image) / width_of_image;

当您知道图像周围的透明空间量时,您可以使用 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.

// To get the number of transparent rows at the top of the image
// Sorry this code is so ugly
uint32 *p = start_of_image;
while ( 0 == *p & 0x000000ff && p < end_of_image_data) ++p;
uint number_of_white_rows_at_top = (p - start_of_image) / width_of_image;

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 :)

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