I want to compare two UIColors that I generated using [UIColor colorWithPatternImage:] for equality. I can't seem to figure out how to do this.
[[UIColor colorWithPatternImage:[UIImage imageNamed:@"camo2"]] isEqual:
[UIColor colorWithPatternImage:[UIImage imageNamed:@"camo2"]]]
Always returns false, whether I use == or isEqual. Does anybody know if it's possible to properly compare colorWithPatternImages, or CGPatterns I suppose? I've also tried comparing CGColorGetPattern(color.CGColor) but that doesn't work either.
EDIT: The reason for this is I have a function that accepts a UIColor and gives me an NSString for displaying to the user.
+(NSString *)colorNameForColor:(UIColor *)color {
if ([color isEqual:[UIColor whiteColor]]) {
return @"White";
}
if ([color isEqual:[UIColor colorWithPatternImage:[UIImage imageNamed:@"camo"]]]) {
return @"Camo";
}
...
}
Is this just an insane thing to do? I suppose I could make my own object that has a color property and a colorName property...
Using Private APIs
This took some reverse engineering of
CoreGraphics
but I was able to find one private method_CGPatternGetImage
which appears to return the image.You'll need to include the following headers:
Create a function pointer:
And then access the function:
You probably don't want to access any private APIs in a production app but this might be useful for testing.
Using Associative References
If this is going on the App Store then a better solution could be creating a category for
UIColor
and give it an associative reference to store the pattern name or whatever is easiest for you to compare. This won't compare the actual images at all so it's possible that if you don't set the correct data to identify the pattern the comparison won't be accurate.Include the header:
Create the category:
And then you can set your custom data and compare: