views:

24

answers:

1

In some code snippet

- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
  CGContextSetFillColorWithColor(ctx, [[UIColor darkTextColor] CGColor]);

  UIGraphicsPushContext(ctx);
  ...

the current fill color is set, then the state is pushed to the stack. Other snippet:

- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
  UIGraphicsPushContext(ctx);

  [[UIColor darkTextColor] set];
  ...

Which way is correct? what's is the difference between those 2 methods CGContextSetFillColorWithColor and UIColor set in terms of state management?

+1  A: 

The only difference is that when you use the UIColor method you can't specify which context to update, it always updates the current context. The UIGraphicsPushContext set a new context to be the current one so of course the order is important. (one more minor difference is that the set method sets both the fill and stroke colors).

Otherwise there is no difference, you can use whichever you like.

In your example I would probably use CGContextSetFillColorWithColor because in that case you don't have to use the UIGraphicsPushContext method.

loomer