Like CGColorRef Car Ads

I have a method that returns a CGColorRef instance created by the CGColorCreate method. I need to auto-detect the color return from this method. Does anyone know how to do this?

//red,green,blue are from 0-255 range

+(CGColorRef) getColorFromRed:(int)red Green:(int)green Blue:(int)blue Alpha:(int)alpha
{
    CGFloat r = (CGFloat) red/255.0;
    CGFloat g = (CGFloat) green/255.0;
    CGFloat b = (CGFloat) blue/255.0;
    CGFloat a = (CGFloat) alpha/255.0;  
    CGFloat components[4] = {r,g,b,a};
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef color = CGColorCreate(colorSpace, components);
    CGColorSpaceRelease(colorSpace);

    //CGColorRelease(color);
    // I need to auto release the color before returning from this.

    return color;
}
+5
source share
4 answers

You cannot directly, as mvds said. Furthermore, UIColorand CGColorRefare not scams without tools - why then the converter works? However (and until I recommend - use UIColorinstead!), There is a trick:

Create an autoreeased object of a UIColor object and return it to CGColor. For instance:

return [UIColor colorWith... ].CGColor;

CGColorRef , UIColor. , UIColor , CGColorRef - CGRetain(...), , . , CGColorRef -...

: . UIColor !

+13

:

CGColorRef color = (CGColorRef)[(id)CGColorCreate(colorSpace, components) autorelease];
+6

. , release, CGColorRef .

, UIColor,

UIColor *ret = [UIColor colorWithCGColor:color]; // ret will be autoreleased
CGColorRelease(color);
return ret;
+3

:

return [(UIColor *)color autorelease];

Or you can use a method + (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alphaand then doreturn [myColor CGColor];

-3
source

All Articles