Nick
Nick

Reputation: 3435

How to create CGColorRef from componets

I have a code line

CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.strokeColor = [UIColor colorWithRed:r green:g blue:b alpha:1.f].CGColor;

Can I rewrite it with our using UIColor? I mean can I create CGColorRef from a, r, g, b components and pass it to shapeLayer.strokeColor, considering using ARC.

Upvotes: 1

Views: 2459

Answers (2)

Mihriban Minaz
Mihriban Minaz

Reputation: 3039

If you want to avoid memory leaks you should release CGColorSpaceRef

CAShapeLayer *shapeLayer = [CAShapeLayer layer];
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGColorRef aColor = CGColorCreate( colorspace, components ); 
shapeLayer.strokeColor = aColor;
CGColorRelease( aColor );
CGColorSpaceRelease(colorspace);

Upvotes: 3

Vishnu gondlekar
Vishnu gondlekar

Reputation: 3956

Yes you can do that. Try out following

CGColorCreate(CGColorSpaceCreateDeviceRGB(), [1.0, 0.5, 0.5, 1.0]) //array values are in order- red, green, blue, alpha

Refer https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGColor/#//apple_ref/c/func/CGColorCreate

Upvotes: 0

Related Questions