0

Hi Can we get hash color string from UIImage ?

In below method if i pass [UIColor redColor] it is working , but if i pass

 #define THEME_COLOR [UIColor colorWithPatternImage:[UIImage imageNamed:@"commonImg.png"]]

then it is not working.

  +(NSString *)hexValuesFromUIColor:(UIColor *)color {

if (CGColorGetNumberOfComponents(color.CGColor) < 4) {
    const CGFloat *components = CGColorGetComponents(color.CGColor);
    color = [UIColor colorWithRed:components[0] green:components[0] blue:components[0] alpha:components[1]];
}
if (CGColorSpaceGetModel(CGColorGetColorSpace(color.CGColor)) != kCGColorSpaceModelRGB) {
    return [NSString stringWithFormat:@"#FFFFFF"];
}
return [NSString stringWithFormat:@"#%02X%02X%02X", (int)((CGColorGetComponents(color.CGColor))[0]*255.0), (int)((CGColorGetComponents(color.CGColor))[1]*255.0), (int)((CGColorGetComponents(color.CGColor))[2]*255.0)];

}

Is there any other methods which can directly get Hash color from UIImage ?

PJR
  • 13,052
  • 13
  • 64
  • 104
  • Do you want to obtain color of individual pixels of image, or one value somehow representing entire picture? – Jakub Vano May 21 '15 at 10:20
  • @JakubVano one value somehow representing entire picture. but I need it in hex string. – PJR May 21 '15 at 10:24

2 Answers2

0

You can't access the raw data directly, but by getting the CGImage of this image you can access it. Reference Link

Community
  • 1
  • 1
Nilesh Patel
  • 6,318
  • 1
  • 26
  • 40
0

You can't do it directly from the UIImage, but you can render the image into a bitmap context, with a memory buffer you supply, then test the memory directly. That sounds more complex than it really is, but may still be more complex than you wanted to hear.

If you have Erica Sadun's iPhone Developer's Cookbook there's good coverage of it from page 54. I'd recommend the book overall, so worth getting that if you don't have it.

I arrived at almost exactly the same code independently, but hit one bug that it looks like may be in Sadun's code too. In the pointInside method the point and size values are floats and are multiplied together as floats before being cast to an int. This is fine if your coordinates are discreet values, but in my case I was supplying sub-pixel values, so the formula broke down. The fix is easy once you've identified the problem, of course - just cast each coordinate to an int before multiplying - so, in Sadun's case it would be:

long startByte = ((int)point.y * (int)size.width) + (int)point.x) * 4;

Also, Sadun's code, as well as my own, are only interested in alpha values, so we use 8 bit pixels that take the alpha value only. Changing the CGBitMapContextCreate call should allow you to get actual colour values too (obviously if you have more than 8 bits per pixel you will have to multiply that in to your pointInside formula too).

OR

Chetan Bhalara
  • 10,326
  • 6
  • 32
  • 51
  • http://stackoverflow.com/questions/556205/how-to-obtain-uicolor-from-a-uiimage-on-iphone – PJR May 21 '15 at 10:22