开发者

Core Graphics state management

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开发者_StackOverflow between those 2 methods CGContextSetFillColorWithColor and UIColor set in terms of state management?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜