加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – 为什么会出现无效的上下文错误?

发布时间:2020-12-15 01:42:04 所属栏目:百科 来源:网络整理
导读:这是我用来绘制的代码: - (void) drawSomething{ CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetRGBStrokeColor(context,1,1); CGContextSetLineWidth(context,6.0); CGContextMoveToPoint(context,100.0f,100.0f); CGContextAddLine
这是我用来绘制的代码:

- (void) drawSomething
{

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(context,1,1);
    CGContextSetLineWidth(context,6.0);

    CGContextMoveToPoint(context,100.0f,100.0f);
    CGContextAddLineToPoint(context,200.0f,200.0f);
    CGContextStrokePath(context);

    NSLog(@"draw");

}

但是我得到了这样的错误:

[Session started at 2010-04-03 17:51:07 +0800.]
Sat Apr  3 17:51:09 MacBook.local MyApp[12869] <Error>: CGContextSetRGBStrokeColor: invalid context
Sat Apr  3 17:51:09 MacBook.local MyApp[12869] <Error>: CGContextSetLineWidth: invalid context
Sat Apr  3 17:51:09 MacBook.local MyApp[12869] <Error>: CGContextMoveToPoint: invalid context
Sat Apr  3 17:51:09 MacBook.local MyApp[12869] <Error>: CGContextAddLineToPoint: invalid context
Sat Apr  3 17:51:09 MacBook.local MyApp[12869] <Error>: CGContextDrawPath: invalid context

为什么它促使我说上下文无效?

解决方法

像 the documentation说:

The current graphics context is nil by default. Prior to calling its drawRect: method,view objects push a valid context onto the stack,making it current.

所以你需要把这段代码放在drawRect方法中

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读