用QuarzCore 框架高度定制一个旋钮,编译器报错:
Mar 2 13:09:55 AVStudyOne[17578]: CGContextResetState: invalid context 0x170162a00. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
解决方法:
1.Xcode工具栏->Product->Scheme->Edit Scheme->Run->Arguments->Envionment Variables中设置CG_CONTEXT_SHOW_BACKTRACE环境变量为YES后,发现并不能解决问题,只不过编译日志打印的是其backtrace。
2.仔细检查代码:
CGContextRef context = UIGraphicsGetCurrentContext();进入文档发现注释如下UIKIT_EXTERN CGContextRef __nullable UIGraphicsGetCurrentContext(void) CF_RETURNS_NOT_RETAINED;
意思就是获取到的CGContext类型的对象,notReitained即在内存管理上其引用计数并没有发生改变。仔细检查过后发现bug所在:
CGContextRelease(context);将此句代码置换为CGContextRestoreGState(context);则编译日志不会再报错误。<前提在之前代码中用CGContextSaveGState>函数存储过context,否则不用做处理。删除CGContextRelease(context);词句代码即可。
再次看文档发现,CGContext类型的对象是通过
/* Push a copy of the current graphics state onto the graphics state stack.
Note that the path is not considered part of the graphics state, and is
not saved. */(会将context存储的到一个状态栈上,并不引起引用计数的变化)
CG_EXTERN void CGContextSaveGState(CGContextRef cg_nullable c)
相应的有一个移出状态栈的函数:
/* Restore the current graphics state from the one on the top of the
graphics state stack, popping the graphics state stack in the process. */
CG_EXTERN void CGContextRestoreGState(CGContextRef cg_nullable c)
网友评论