开发者

programmatically trimming (automatically cropping out transparent boundaries) an image in objective-c / cocoa

开发者 https://www.devze.com 2022-12-23 04:31 出处:网络
Does anyone know how to trim an image (uiimage or cgimage). By trim I mean programatica开发者_JAVA技巧lly cropping to the non-transparent bounds of an image. So if I have the image below:

Does anyone know how to trim an image (uiimage or cgimage). By trim I mean programatica开发者_JAVA技巧lly cropping to the non-transparent bounds of an image. So if I have the image below:

  • 00111000
  • 00010000
  • 01011110
  • 00000000

it would yield:

  • 011100
  • 001000
  • 101111


Sum all rows and all columns of your image. You'll get two arrays, in your example looking like this:

3 1 5 0
0 1 1 3 2 1 1 0

Then the first non-zero from the left and the last one near the right is where you have to crop, in each direction.

0

精彩评论

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