iOS报错 - If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variabl

Error: CGContextSetLineCap: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

解决方法:

需要在plist文件中增加如下设置
这里写图片描述
将这个删除 或者 改为YES
(没有这个,就是默认YES)

原因:
iOS9系统的一些不兼容
设置 app 的状态栏样式的时候,使用了旧的方式,在 info.plist 里面的 View controller-based status bar appearance 默认会为 YES,即使不设置也是 YES,但一般 iOS6 的时候为了设置状态栏样式,需要将其设为NO,iOS7,8也兼容,但是到了iOS9 就会报警告。

然而问题并未就此解决

如此设置以后,这样的报错还是存在!!!!!!
后来发现,


#import "Line.h"
@interface Line(){

    CGContextRef _context;

}@end


@implementation Line

-(instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {
        [self setInit];
    }return self;
}


-(void)setInit{

    _with = @(5);
    _color = [UIColor colorWithRed:233/255 green:98/255 blue:64/255 alpha:1];

        _context = UIGraphicsGetCurrentContext();
}

-(void)drawRect:(CGRect)rect{



    CGContextSetLineWidth(_context, [_with doubleValue]);
    CGContextSetLineCap(_context, kCGLineCapRound);
    CGContextMoveToPoint(_context, 0, self.frame.size.height/2);
    CGContextAddLineToPoint(_context, self.frame.size.width, self.frame.size.height/2);
    CGContextSetFillColorWithColor(_context, _color.CGColor);
    CGContextStrokePath(_context);

这段代码中, 关于CGContextRef的初始化,

 _context = UIGraphicsGetCurrentContext();

是处于init方法中执行的,
于是怀疑是否是draw方法执行的顺序问题
百度之
有这么一句解释

若使用UIView绘图,只能在drawRect:方法中获取相应的contextRef并绘图。如果在其他方法中获取将获取到一个invalidate 的ref并且不能用于画图

于是,修改下初始化CGContextRef初始化的位置

#import "Line.h"
@interface Line(){

    CGContextRef _context;

}@end


@implementation Line

-(instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {


        [self setInit];
    }return self;
}


-(void)setInit{

    _with = @(5);
    _color = [UIColor colorWithRed:233/255 green:98/255 blue:64/255 alpha:1];


}

-(void)drawRect:(CGRect)rect{

    //初始化
    _context = UIGraphicsGetCurrentContext();


    CGContextSetLineWidth(_context, [_with doubleValue]);
    CGContextSetLineCap(_context, kCGLineCapRound);
    CGContextMoveToPoint(_context, 0, self.frame.size.height/2);
    CGContextAddLineToPoint(_context, self.frame.size.width, self.frame.size.height/2);
    CGContextSetFillColorWithColor(_context, _color.CGColor);
    CGContextStrokePath(_context);




}

终于解决,且获得自己所需要的绘图图形!
这里写图片描述

更多iOS9系统的更新配置 或则报错 可以百度这方面相关的帖子,
写这个纯粹是因为自己也遇到了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值