我需要得到一个修剪过的 CGImage。我有一个图像,在某些颜色周围有空白空间(alpha = 0),需要对其进行修剪以获得仅可见颜色的大小。
谢谢。
我需要得到一个修剪过的 CGImage。我有一个图像,在某些颜色周围有空白空间(alpha = 0),需要对其进行修剪以获得仅可见颜色的大小。
谢谢。
有三种方法可以做到这一点:
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 设置为 center 并让它为您进行修剪:)