iOS繪圖出現(xiàn)的錯誤

今天使用貝塞爾曲線運行后出現(xiàn)無效上下文錯誤,這里記錄一下。
先上代碼:

-(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.

上網(wǎng)查找錯誤的原因,大部分都說是IOS9適配問題,設(shè)置app的狀態(tài)欄樣式的使用使用了舊的方式在IOS9中不兼容。但是我的工程中并沒有設(shè)置狀態(tài)欄,并且按照其解決方法更改后也沒有解決這個問題。后來看到這篇文章H含金量 iOS繪圖及貝塞爾曲線關(guān)鍵知識才知道問題所在。
因為繪圖不在drawRect:方法中操作導(dǎo)致繪圖時沒有當(dāng)前的圖形上下文(context)可設(shè)置。所以應(yīng)該在drawRect:中執(zhí)行圖形繪制。修改后代碼如下:

-(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];
}

這樣就不會再出現(xiàn)錯誤了。其實我這里的path是UIBezierPath,可以直接去掉[self.path stroke];和[self.path fill];這兩處的。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容