‘无效上下文 0x0’使用 CGContext* 函数时出错
/* 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 所说的去做,但我没有得到任何东西。 我不断收到此错误:
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。
我认为我对 CGContext 的理解不够好,也不知道 CGContextRef 应该是什么。
/* 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);
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
I reference the CGContextRef in my header file.
i don't think i understand CGContext well enough nor do i know what CGContextRef should be.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了使您的代码正常工作,需要在
UIView
子类的drawRect
方法中执行(并且您不直接调用drawRect
-当操作系统需要 UIView 来渲染自身时,它会被调用)。您可能正在尝试从触摸事件或 viewDidLoad 或类似的事件运行此代码。For your code to work, it needs to be executed in the
drawRect
method of yourUIView
subclass (and you don't calldrawRect
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 fromviewDidLoad
or something like that.