gpt4 book ai didi

ios - CGContextSaveGState 与 UIGraphicsPushContext

转载 作者:IT王子 更新时间:2023-10-29 07:59:14 25 4
gpt4 key购买 nike

drawRect方法有两种:

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

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

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

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

最佳答案

UIGraphicsPushContext(context) 将上下文插入 CGContextRefs 堆栈(使上下文成为当前绘图上下文),而 CGContextSaveGState(context) 将当前图形状态插入堆栈图形状态上下文维护。如果您需要使新的 CGContextRef 成为当前绘图上下文,您应该使用 UIGraphicsPushContext,并且当您使用一个图形上下文并且只想保存时,您应该使用 CGContextSaveGState,例如:当前变换状态、填充或描边颜色,等等

关于ios - CGContextSaveGState 与 UIGraphicsPushContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15505871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com