ahow
ahow

Reputation: 57

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.

Upvotes: 0

Views: 1416

Answers (1)

Seamus Campbell
Seamus Campbell

Reputation: 17906

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];

Upvotes: 1

Related Questions