'invalid context 0x0'错误时使用CGContext *函数 [英] ‘invalid context 0x0’ error when using CGContext* functions
问题描述
/* Adding the Path */
UserGraphBuff = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(UserGraphBuff,5,10,0,1);
CGContextSetLineWidth(UserGraphBuff, 2 );
CGContextBeginPath(UserGraphBuff);
//line to last user point
CGContextAddLineToPoint(UserGraphBuff, (*xVal)[sizeof xVal / sizeof *xVal - 1], (*yNewVal)[sizeof yNewVal / sizeof *yNewVal - 1]);
//line to rest of user points in reverse order
for (int i = sizeof xVal / sizeof *xVal - 1; i > -1; i--){
CGContextAddLineToPoint(UserGraphBuff, (*xVal)[i], (*yNewVal)[i]);
}
//EOFill
CGContextEOFillPath(UserGraphBuff);
上面是我想要通过的代码。它应该做什么CGContext说它,但我没有得到任何东西绘制。
我一直收到此错误:
Above is the code I am trying to work through. Its supposed to do what CGContext says it does but I am not getting any thing drawn. I keep getting this error:
Fri Oct 28 13:18:40 case.app testApplication[4127] <Error>: CGContextSetRGBStrokeColor: invalid context 0x0
Fri Oct 28 13:18:40 case.app testApplication[4127] <Error>: CGContextSetLineWidth: invalid context 0x0
Fri Oct 28 13:18:40 case.app testApplication[4127] <Error>: CGContextBeginPath: invalid context 0x0
Fri Oct 28 13:18:40 case.app testApplication[4127] <Error>: CGContextAddLineToPoint: invalid context 0x0
Fri Oct 28 13:18:40 case.app testApplication[4127] <Error>: CGContextDrawPath: invalid context 0x0
我引用我的头文件中的CGContextRef。
I reference the CGContextRef in my header file.
我不认为我理解CGContext够好,也不知道CGContextRef应该是什么。
i don't think i understand CGContext well enough nor do i know what CGContextRef should be.
推荐答案
为了让代码起作用,需要在 UIView $ c>的
drawRect
$ c> subclass(并且你不直接调用 drawRect
- 当需要UIView来渲染自己时,它被操作系统调用)。你可能试图从触摸事件或从 viewDidLoad
或类似的东西运行此代码。
For your code to work, it needs to be executed in the drawRect
method of your UIView
subclass (and you don't call drawRect
directly - this is called by the OS when it needs the UIView to render itself). You're presumably trying to run this code from a touch event or from viewDidLoad
or something like that.
这篇关于'invalid context 0x0'错误时使用CGContext *函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!