开发者

Deciphering CGColor

Hi Im trying to get my head around CGColor, Ive been doing a lot of Quartz2d stuff. In the past I've fudged it with [UIColor xColor].CGColor.... but with this new compiler I get crashes when i specify the color, ie [UIColor redColor].CGColor is fine but if I cook my own [UIColor color withRed: x Green:y etc].CGColor it crashes. I dont think its a retain issue because [UIColor alloc]initWithRed:].CGColor gives me the same thing. I'm guessing the UIColor -> CGColor bridge is kinda fudged, hard coded in for the preset colors only, not a conversion. And so I'm looking for an RGB type CGColor scheme so I can reconstruct a color from an array of values. I dont really care if its 0-1 or 1-255 etc, just need 开发者_高级运维a system I can get my head around if anyone can throw me a bone I'd really appreciate it cheers


I expect what's really going on is you aren't retaining the resulting color, and you're letting the UIColor get autoreleased. The built-in colors (e.g. -redColor, etc.) are likely globals that live forever, but the ones you create with -colorWithRed:green:blue:alpha: are created on-demand and autoreleased. If you pull the CGColor out of it, the CGColor's lifetime is tied to the lifetime of the UIColor, so if you're going to hold onto the CGColor past the current stack you'll need to CGColorRetain() it (and then CGColorRelease() it when you're done).


You can create it with CGColorRef CGColorCreateGenericRGB(CGFloat red,CGFloat green,CGFloat blue,CGFloat alpha);

details here: http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Reference/CGColor/Reference/reference.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜