开发者

Trim alpha from CGImage

I need to get a trimmed CGImage. I have an image which has empty space 开发者_如何学JAVA(alpha = 0) around some colors and need to trim it to get the size of only the visible colors.

Thanks.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜