CGContextSetStrokeColorWithColor: invalid context 0x0.错误

Apr  6 15:29:40  testDemo[4362] <Error>: CGContextSetStrokeColorWithColor: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.```

![错误信息](http://upload-images.jianshu.io/upload_images/701353-9a51f1c00e79eee5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

在绘图的时候或者使用贝塞尔曲线的时候会在控制台打印出上面的日志。
1.报错名称 :` CGContextSaveGState: invalid context 0x0.  CGContextDrawPath: invalid context 0x0.`等。我这里编译时并没有报错也无警告,直到运行时才在控制台打印了一系列的<Error>信息 如下图(错误信息)
出现这类错误的原因是没有在` view` 的` -drawRect:`  方法中对` layer` 进行了不适当的操作 例如` UIBezierPath `的 ```- (void)setFill; - (void)setStroke; 方法  UIColor 的 - (void)setFill; - (void)setStroke;```方法 等。
2.解决方法
(1).把有关于`view layer` 层的操作单独封装到一个 view 类的 `-drawRect: `方法中
(2).不要调用 相关的方法 ,官方一般都给了注释,cmd+单击 就可查看:如下图(官方给定的限制->drawing context)
![](http://upload-images.jianshu.io/upload_images/701353-e5c1ef53f5e4a232.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)官方给定的限制->drawing context
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容