Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGContextSaveGState vs UIGraphicsPushContext

Tags:

There are two drawRect methods:

- (void)drawRect:(CGRect)rect {     CGContextRef context = UIGraphicsGetCurrentContext();     CGContextSaveGState(context);     // do drawing here     CGContextRestoreGState(context); } 

And

- (void)drawRect:(CGRect)rect  {     CGContextRef context = UIGraphicsGetCurrentContext();     UIGraphicsPushContext(context);     // do drawing here     UIGraphicsPopContext();  } 

UIGraphicsPushContext / UIGraphicsPopContext are from UIKit while CGContextSaveGState / CGContextRestoreGState are from CoreGraphics.

Questions: What is the difference between those methods? Which one is better to use? Are there some examples of proving one method better than other and vise versa?

like image 521
Vadim Avatar asked Mar 19 '13 17:03

Vadim


1 Answers

UIGraphicsPushContext(context) pushes context onto a stack of CGContextRefs (making context the current drawing context), whereas CGContextSaveGState(context) pushes the current graphics state onto the stack of graphics states maintained by context. You should use UIGraphicsPushContext if you need to make a new CGContextRef the current drawing context, and you should use CGContextSaveGState when you're working with one graphics context and just want to save, for example: the current transform state, fill or stroke colors, etc.

like image 139
Aaron Golden Avatar answered Sep 29 '22 13:09

Aaron Golden