Alex Rivera | Logout

UIColor - getRed: ... - raises exception

Asked 2011-11-06T09:32:00.773
10

I have the following test code:

CGFloat endRed, endGreen, endBlue, endAlpha;

[[UIColor greenColor] getRed:&endRed green:&endGreen blue:&endBlue alpha:&endAlpha];

that I call inside drawRect method for an UIView class. This code fails with exception

2011-11-06 02:29:28.671 Chartous[13457:b303] -[UICachedDeviceRGBColor getRed:green:blue:alpha:]: unrecognized selector sent to instance 0x4e7ea10
2011-11-06 02:29:28.673 Chartous[13457:b303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UICachedDeviceRGBColor getRed:green:blue:alpha:]: unrecognized selector sent to instance 0x4e7ea10'

What is wrong here?

Edit
Report

1 Answer

4

In the case of monotone colors, the number of components is 2 so you need another check unfortunately

E.g. [UIColor whitecolor] has only 2 components for alpha and the color

size_t num = CGColorGetNumberOfComponents(cgPrevColor);
CGFloat r,g,b;
if(num==2) {
    r = g = b = components[0];
} else {
    r = components[0];
    g = components[1];
    b = components[2];            
}
answered 2012-11-06T01:58:13.763

Your Answer