Extracting CGColor out of UIColor - ios

I have a UIColor initiated with [UIColor colorWithRed:Green:Blue:Alpha] and I want to extract its corresponding CGColor to use with CGContextSetFillColorWithColor.
I tried [[UIColor colorWithRed...] CGColor] but for some reason I get no coloring.I
[[UIColor colorWithRed:216 green:156 blue:1 alpha:1] CGColor]
The weird thing is that when I use:
[[UIColor orangeColor] CGColor]
It works just fine.
Any ideas?
Thanks!
EDIT:
The actual code:
CGRect rect = CGRectMake(0.0, 0.0, width, height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor colorWithRed:216 green:156 blue:1 alpha:1].CGColor);
CGContextFillRect(context, rect);
self.image = UIGraphicsGetImageFromCurrentImageContext();
I don't think its code-related - again, if I change the UIColor to a constant color it all works.
The purpose of the code is to create an UIImage filled with a specific color.

Related

What's the difference between CGContextSetStrokeColorWithColor UIColor setStroke?

Is there any difference between those two ways of setting stroke color?
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor)
[[UIColor redColor] setStroke]
Example
CGContextRef cr = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(cr, [UIColor redColor].CGColor);
[[UIColor redColor] setStroke];
UIBezierPath *bp = [UIBezierPath new];
[bp stroke];
UIGraphicsEndImageContext();
One is longer and pure C. The other is shorter and uses Objective-C. They have the same effect.

"Potential leak of an object" when using Core Graphics

My app is drawing a shadow using the following code:
-(void)drawShadow:(CGContextRef)context rect:(CGRect)rect{
CGContextSaveGState(context);
//Set color of current context
[[UIColor blackColor] set];
//Set shadow and color of shadow
CGContextSetShadowWithColor(context, CGSizeMake(0, 2), 3, [[UIColor colorWithWhite:0 alpha:0.5] CGColor]);
CGContextFillEllipseInRect(context, rect);
CGContextClipToMask(context, rect, CGBitmapContextCreateImage(context));
CGContextRestoreGState(context); // Warning shows in this line
}
When I run Product > Analyze, it marks the last instruction of this block with the message: "Potential leak of an object". When I remove that line, it shows the same message but in the closing bracket.
Any idea of what I'm doing wrong?
Thanks
I think it's the CGBitmapContextCreateImage that is leaking.
Try assigning it to a local variable and then calling CGImageRelease with it once you've clipped to mask.
Another iPhone - CGBitmapContextCreateImage Leak
chedabob's answer worked! Here's the final code:
-(void)drawShadow:(CGContextRef)context rect:(CGRect)rect{
CGContextSaveGState(context);
//Set color of current context
[[UIColor blackColor] set];
//Set shadow and color of shadow
CGContextSetShadowWithColor(context, CGSizeMake(0, 2), 3, [[UIColor colorWithWhite:0 alpha:0.5] CGColor]);
CGContextFillEllipseInRect(context, rect);
CGImageRef bitmap = CGBitmapContextCreateImage(context);
CGContextClipToMask(context, rect, bitmap);
CGContextRestoreGState(context);
CGImageRelease(bitmap);
}

Why am I getting the wrong colour here?

The code below results in a pure red square:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetLineWidth(context, 5.0f);
CGContextStrokeRect(context, rect);
}
But the code below results in a white square when what I'd expect is a different shade of red. Can anyone tell me why this is?
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor* boxColour = [UIColor colorWithRed:217 green:37 blue:70 alpha:1.0];
CGContextSetStrokeColorWithColor(context, boxColour.CGColor);
CGContextSetLineWidth(context, 5.0f);
CGContextStrokeRect(context, rect);
}
The colorWithRed:green:blue:alpha: method of the UIColor class takes parameters in the range 0.0 to 1.0. You probably just need to divide all the values (except alpha) by 255.0f:
UIColor* boxColour = [UIColor colorWithRed:217/255.0f green:37/255.0f blue:70/255.0f alpha:1.0f];
Apple Documentation

CGContextSetFillColorWithColor not working with passed in color variable

I'm trying to color an image using CGContextSetFillColorWithColor and a color variable I'm grabbing from my Parse database.
Changing the color of the image this way works fine:
[...]
UIImage *image = [UIImage imageNamed:#"menuButton.png"];
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClipToMask(context, rect, image.CGImage);
CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
[...]
That changes the menuButton.png to white no problem.
But when I try to change it to the hex color I'm getting from Parse (using a hex color utility) this way:
self.bgColor = [post objectForKey:#"bgColor"]; //bgColor = "#ffffff"
[...]
CGContextSetFillColorWithColor(context,
[[UIColor colorWithHexString:self.bgColor] CGColor]);
The image no longer appears. Is this a pointer issue? Thanks for your help.
I have had a similar problem and didn't find a direct solution. The solution for me was to use the -setFill method on my UIColor object, rather than using the intermediate CGColor object:
[[UIColor colorWithHexString:self.bgColor] setFill];

Quartz 2D Drawing Crashing

I don't know why this stopped working, but this chunk of code is crashing my device when I try drawing any part of it. I'm new to core graphics so any pointers or suggestions would be a great help. Thanks!
// Style
CGContextRef context = UIGraphicsGetCurrentContext();
// Colors
CGColorRef fillBox = [UIColor colorWithRed:250.0/255.0 green:250.0/255.0 blue:250.0/255.0 alpha:1.0].CGColor;
CGColorRef fillBoxShadow = [UIColor colorWithRed:77.0/255.0 green:77.0/255.0 blue:77.0/255.0 alpha:1.0].CGColor;
CGRect box = CGRectMake(5, 5, self.frame.size.width - 10, self.frame.size.height - 10);
// Shadow
CGContextSetShadowWithColor(context, CGSizeMake(0, 0), 1.0, fillBoxShadow);
CGContextAddRect(context, box);
CGContextFillPath(context);
// Box
CGContextSetFillColorWithColor(context, fillBox);
CGContextAddRect(context, box);
CGContextFillPath(context);
If your project is using ARC, then these two lines might be part of your problem:
CGColorRef fillBox = [UIColor colorWithRed:250.0/255.0 green:250.0/255.0 blue:250.0/255.0 alpha:1.0].CGColor;
CGColorRef fillBoxShadow = [UIColor colorWithRed:77.0/255.0 green:77.0/255.0 blue:77.0/255.0 alpha:1.0].CGColor;
ARC is releasing the UIColor object and thus the CGColorRef. You need to retain the CGColorRef and then release it when you are done with it.
I would write the code something like this:
UIColor *fillBox = [UIColor colorWithRed:250.0/255.0 green:250.0/255.0 blue:250.0/255.0 alpha:1.0];
UIColor *fillBoxShadow = [UIColor colorWithRed:77.0/255.0 green:77.0/255.0 blue:77.0/255.0 alpha:1.0];
and then use fillBox.CGColor and fillBoxShadow.CGColor later on in the method.
Instead of doing
CGContextAddRect(context, box);
CGContextFillPath(context);
Try using CGContextFillRect.
You can't fill a path you haven't added to the context beforehand.
Note: you could do
CGContextAddPath(context, [UIBezierPath pathWithRect:box].CGPath);
CGContextFillPath(context);
but that's a bit overkill compared to just filling a rect.
(Not sure about the pathWithRect: syntax, but it exists.)

Resources