user1542125 user1542125 - 5 months ago 21
Objective-C Question

Difference between stock UIColor and custom UIColor

I'm trying to get RGB values from a UIColor object.

If I do the following on a UIColor object it works

UIColor *top = [UIColor lightGrayColor];
const CGFloat* colorsTop = 0;
colorsTop = CGColorGetComponents(top.CGColor);


Top comes out as: UICachedDeviceRGBColor * 0x7a7d5b50

However, if I do the following it fails. Can someone please explain why?

UIColor *top = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
const CGFloat* colorsTop = 0;
colorsTop = CGColorGetComponents(top.CGColor);


Top comes out as: UIColor * 0x7a7d5cb0

I'm getting error on the last one: EXC_BAD_ACCESS (code=1, 0xb7a7d5b9)

Answer

To get the RGB components of a UIColor you should be using this method...

- (BOOL)getRed:(CGFloat *)red green:(CGFloat *)green blue:(CGFloat *)blue alpha:(CGFloat *)alpha

Like this...

CGFloat red = 0;
CGFloat green = 0;
CGFloat blue = 0;
CGFloat alpha = 0;

[someUIColor getRed:&red green:&green blue:&blue alpha:&alpha];

red, green, blue, and alpha will now contain the values of that colour.