views:

349

answers:

1

Does anyone know how to trim an image (uiimage or cgimage). By trim I mean programatically 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
+1  A: 

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.

AVB
that's brilliant. thanks very much.
Ferris