gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 03:35:01 25 4
gpt4 key购买 nike

这是我用来绘制的代码:

- (void) drawSomething
{

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 1, 0, 0, 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方法中

关于iphone - 为什么会出现无效上下文错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2570846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com