Ios CGContextSaveGState与UIGraphicsPushContext

Ios CGContextSaveGState与UIGraphicsPushContext,ios,objective-c,drawrect,cgcontext,Ios,Objective C,Drawrect,Cgcontext,有两种drawRect方法: - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSaveGState(context); // do drawing here CGContextRestoreGState(context); } 及 UIGraphicsPushContext/UIGraphicsPopContext来自UI

有两种drawRect方法:

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

UIGraphicsPushContext/UIGraphicsPopContext来自UIKit 而CGContextSaveGState/CGContextRestoreGState则来自CoreGraphics


问题:这些方法之间有什么区别?哪一个更好用?是否有一些例子证明一种方法优于另一种方法,反之亦然

UIGraphicsPushContext(context)
将上下文推送到cgcontextref堆栈上(使上下文成为当前图形上下文),而
CGContextSaveGState(context)
将当前图形状态推送到由上下文维护的图形状态堆栈上。如果需要将新的CGContextRef设置为当前图形上下文,则应使用UIGraphicsPushContext,并且在处理一个图形上下文并仅希望保存时,应使用CGContextSaveGState,例如:当前变换状态、填充或笔划颜色等。

UIGraphicsPushContext(ctx)如果要使用UIkit绘制,而当前上下文不是要绘制的上下文,则此函数非常有用。可以使用此函数将要绘制的上下文变为当前上下文。 CGContextSaveGState(ctx)保存上下文(由ctx引用),以后可以使用CGContextRestoreGState()还原上下文

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