21 July 2010 by Published in: Uncategorized No comments yet

I discovered the weirdest behavior *ever* today.

	#define DRAW_COLOR [UIColor redColor]
CGContextSetStrokeColor(context, CGColorGetComponents([DRAW_COLOR CGColor]));

For some bizarre reason, this line of code works fine as long as you are drawing into a “screen” context (e.g. one set up by drawRect).

If you are drawing to a bitmap context (e.g. UIGraphicsBeginImageContext), it will for some random reason set the stroke color to transparent (WTF?)

The correct way (and as far as I can tell, nearly equivalent) to get drawing behavior to work for both types of contexts is this:

	CGContextSetStrokeColorWithColor(context, [DRAW_COLOR CGColor]);

The only conclusion that I can come to is that somehow the colorspace for bitmap contexts and screen contexts are slightly different. This is because the buggy line requires the device to be using an rgba colorspace, whereas I don’t think the second line depends on the colorspace or colorcomponents of the color.


Want me to build your app / consult for your company / speak at your event? Good news! I'm an iOS developer for hire.

Like this post? Contribute to the coffee fund so I can write more like it.

Comments

Comments are closed.

Powered by WordPress