2011-10-28 57 views
5
/* 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说它做,但我没有得到任何东西绘制。 我不断收到此错误:使用CGContext *功能时出现'无效上下文0x0'错误

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

+0

此代码位于何处?在UIView子类的'drawRect'中? – MusiGenesis

+0

' - (void)userShow {' –

+1

那是什么?它有什么作用?它是否被'-drawRect:'调用? –

回答

17

为您的代码的工作,它需要你的UIView子类的drawRect方法来执行(你不直接调用drawRect - 这是通过操作系统调用时它需要的UIView来表现自己)。你可能试图从触摸事件或从viewDidLoad或类似的东西运行此代码。

+0

它在自己的几件事情的方法。它不是单独称呼的。我从检查事件中运行这个。我希望用户数据与实际绘图(实际上是一个跟踪游戏)交叉引用。在这一点上,我只是试图找出一个人所做的准备。 –

+6

重申:除非它在'UIView'子类的'drawRect'方法中运行,否则你的代码将无法工作(意味着它会抛出错误)。 – MusiGenesis

+4

并解释:UIView在调用你的'drawRect:'之前设置当前上下文。这是一个功能:当需要绘图时绘制,系统会为您跟踪。不要反对;将您的绘图代码移动到应有的位置,然后使用系统。当视图需要重绘的某些内容发生变化时,将该视图设置为需要显示。 –

相关问题