- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
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/
正如苹果文档所说,UIGraphicsGetCurrentContext只能在drawRect方法中使用。如果你想在其他地方使用它,你必须先推送一个上下文。 现在我想使用 UIGraphicsGetC
我在 MBProgressHUD ( https://github.com/jdg/MBProgressHUD ) 中注意到下面的代码 - (void)drawRect:(CGRect)rect {
drawRect方法有两种: - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContex
理解 UIGraphicsPushContext(context) 和 UIGraphicsPopContext() 非常令人沮丧 我的理解是,我可以设置上下文的属性,例如笔触颜色,然后将上下文压入堆
我是一名优秀的程序员,十分优秀!