Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trimming colour issues #12

Open
runmad opened this issue Nov 18, 2013 · 1 comment
Open

Trimming colour issues #12

runmad opened this issue Nov 18, 2013 · 1 comment

Comments

@runmad
Copy link

runmad commented Nov 18, 2013

Thanks for this library. However, I seem to have issues using the trimming utility. I assign a UIColor with RGB colour space, but it doesn't actually trim the image.

Here's an example of an image I am trying to trim:

arcadefire

I'm wanting to trim the black borders on the top and bottom, so I set the UIColor to [UIColor colorWithRed:0 green:0 blue:0 alpha:1]] but I am returned the exact same image, nothing is being trimmed.

@cameronehrlich
Copy link

I believe the problem is that starting right here the code assumes that black and white are represented with 4 components like all the other colors in the RGBA colorspace. After debugging and hunting around I figured out that black and white are represented by a seemingly undocumented color space kCGColorSpaceDeviceGray which causes this to evaluate to true: CGColorGetNumberOfComponents([UIColor black].CGColor) == 2

Because of this, line 40 returns NULL. Additionally, the assumptions made in line 48 will not produce the expected outcome when trying to crop out black or white borders.

I hacked together a UIImage category today that I believe handles this issue correctly. I intend to expand upon it to be more full featured for use in an app I am working on, but feel free to borrow and/or contribute back!

https://github.com/cameronehrlich/ImageTrim/blob/master/ImageTrim/UIImage%2BImageTrim.m

Possible things I would like to add are:

  • Auto-Detection of border color
  • Better color tolerance logic
  • The ability to set a minimum width or height that can be cropped to so as to avoid weird image slivers.
  • The option to choose which sides you'll apply the trimming to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants