iOS绘图出现的错误

作者: Lycho | 来源:发表于2016-09-09 16:19 被阅读5567次

今天使用贝塞尔曲线运行后出现无效上下文错误,这里记录一下。
先上代码:

-(void)createCircle
{
CGPoint layerCenter = CGPointMake(CGRectGetWidth(self.view.frame)/2, CGRectGetWidth(self.view.frame)/2);
CAShapeLayer *layer = [CAShapeLayer layer];
layer.frame = self.view.bounds;
layer.lineWidth = 6.0;
layer.strokeColor = [UIColor redColor].CGColor;
layer.fillColor = [UIColor whiteColor].CGColor;
self.path = [UIBezierPath bezierPath];
[self.path addArcWithCenter:layerCenter radius:50 startAngle:0.0 endAngle:M_PI*2 clockwise:YES];
[self.path stroke];
[self.path fill];
layer.path = self.path.CGPath;
[self.view.layer addSublayer:layer];
}

错误日志如下:

Sep  9 16:09:01  Test1[28072] <Error>: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Sep  9 16:09:01  Test1[28072] <Error>: CGContextSetLineWidth: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Sep  9 16:09:01  Test1[28072] <Error>: CGContextSetLineJoin: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Sep  9 16:09:01  Test1[28072] <Error>: CGContextSetLineCap: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Sep  9 16:09:01  Test1[28072] <Error>: CGContextSetMiterLimit: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Sep  9 16:09:01  Test1[28072] <Error>: CGContextSetFlatness: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Sep  9 16:09:01  Test1[28072] <Error>: CGContextAddPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Sep  9 16:09:01  Test1[28072] <Error>: CGContextDrawPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Sep  9 16:09:01  Test1[28072] <Error>: CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Sep  9 16:09:01  Test1[28072] <Error>: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Sep  9 16:09:01  Test1[28072] <Error>: CGContextSetFlatness: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Sep  9 16:09:01  Test1[28072] <Error>: CGContextAddPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Sep  9 16:09:01  Test1[28072] <Error>: CGContextDrawPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
Sep  9 16:09:01  Test1[28072] <Error>: CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

上网查找错误的原因,大部分都说是IOS9适配问题,设置app的状态栏样式的使用使用了旧的方式在IOS9中不兼容。但是我的工程中并没有设置状态栏,并且按照其解决方法更改后也没有解决这个问题。后来看到这篇文章H含金量 iOS绘图及贝塞尔曲线关键知识才知道问题所在。
因为绘图不在drawRect:方法中操作导致绘图时没有当前的图形上下文(context)可设置。所以应该在drawRect:中执行图形绘制。修改后代码如下:

-(void)createCircle
{
CGPoint layerCenter = CGPointMake(CGRectGetWidth(self.view.frame)/2, CGRectGetWidth(self.view.frame)/2);
CAShapeLayer *layer = [CAShapeLayer layer];
layer.frame = self.view.bounds;
layer.lineWidth = 6.0;
layer.strokeColor = [UIColor redColor].CGColor;
layer.fillColor = [UIColor whiteColor].CGColor;
self.path = [UIBezierPath bezierPath];
[self.path addArcWithCenter:layerCenter radius:50 startAngle:0.0 endAngle:M_PI*2 clockwise:YES];
//    [self.path stroke];
//    [self.path fill];
layer.path = self.path.CGPath;
[self.view.layer addSublayer:layer];
[self.view setNeedsDisplay];
}
- (void) drawRect:(CGRect)rect {
[self.path stroke];
[self.path fill];
}

或者改成这样:

-(void)createCircle
{
    CGPoint layerCenter = CGPointMake(CGRectGetWidth(self.view.frame)/2, CGRectGetWidth(self.view.frame)/2);
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.frame = self.view.bounds;
    layer.lineWidth = 6.0;
    layer.strokeColor = [UIColor redColor].CGColor;
    layer.fillColor = [UIColor whiteColor].CGColor;
    
    self.path = [UIBezierPath bezierPath];
    [self.path addArcWithCenter:layerCenter radius:50 startAngle:0.0 endAngle:M_PI*2 clockwise:YES];
        layer.path = self.path.CGPath;
    
    UIGraphicsBeginImageContext(self.view.bounds.size);
    [self.path stroke];
    [self.path fill];
    UIGraphicsEndImageContext();
    
    [self.view.layer addSublayer:layer];
}

这样就不会再出现错误了。其实我这里的path是UIBezierPath,可以直接去掉[self.path stroke];和[self.path fill];这两处的。

相关文章

  • iOS绘图出现的错误

    今天使用贝塞尔曲线运行后出现无效上下文错误,这里记录一下。先上代码: 错误日志如下: 上网查找错误的原因,大部分都...

  • CAD绘图标注出现错误数值的调整方法!

    CAD绘图标注出现错误数值的调整方法! CAD绘图标注出现错误数值的调整方法!

  • iOS绘图详解(链接)

    iOS绘图详解iOS绘图教程

  • iOS 绘图

    转自:iOS绘图—— UIBezierPath 和 Core Graphics绘图进阶请参考:绘图 前言 iOS系...

  • iOS绘图框架CoreGraphics分析

    iOS绘图框架CoreGraphics分析 iOS绘图框架CoreGraphics分析

  • IndexError: list index out of ra

    在今晚练习Matplotlib读取CSV文件并绘图的过程中,出现错误: IndexError: list inde...

  • IOS 学习之绘图( Core Graphics 教学)

    IOS 绘图 总结 Core Graphics IOS中绘图的三种方式 在UIKit控件中,的drawInReat...

  • 2019-07-07

    Thread 1: signal SIGABRT 错误 今天尝试swift ios编程,出现上述错误。 具体原因是...

  • ios绘图基础

    ios绘图才一些场合很好用,这里演示一些基本的方法。 -1 ios绘图基础 -2 ios常见的图形绘制 代码下载:...

  • 绘图

    IOS中绘图的方式介绍 IOS中貌似绘图的方式还挺多的,有 Core Graphics/QuartZ 2D UIK...

网友评论

  • 阳光下的泡泡:我也遇到这个问题了,可以加一下你的QQ吗?
  • ca0c0c690c44:你好,能加下qq吗?有个问题咨询
  • 国王or乞丐:QQ:1030554941
  • 国王or乞丐:楼主,呜呜,一周搞了两个图,还出现了这个问题,懵逼:sob:
  • 国王or乞丐:LZ,你好,可否加下QQ,我这边也遇到了这个问题,但是我选用的是第三方,也不清楚如何给他去做这个处理,我写了个Dreaw,也还是不管用
    国王or乞丐:@Lycho 1030554941
    Lycho:你把你的qq给我吧,我加一下你

本文标题:iOS绘图出现的错误

本文链接:https://www.haomeiwen.com/subject/mpgeettx.html