gpt4 book ai didi

cocoa-touch - 使用 CGContext* 函数时出现“无效上下文 0x0”错误

转载 作者:行者123 更新时间:2023-12-04 03:06:21 27 4
gpt4 key购买 nike

/*  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 应该是什么。

最佳答案

要使您的代码正常工作,它需要在 drawRect 中执行您的方法 UIView子类(并且您不直接调用 drawRect - 当操作系统需要 UIView 来呈现自己时,它会调用它)。您可能正在尝试从触摸事件或从 viewDidLoad 运行此代码或类似的东西。

关于cocoa-touch - 使用 CGContext* 函数时出现“无效上下文 0x0”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7932654/

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