2011-04-13 86 views
1

对于苹果说我们不能在UIImageView中使用drawRec:。所以我决定使用CALayer来完成这项工作,并将图层添加到UIImageView.layer中。 我做了一个名为MyCALayer类扩展的CALayer类,我覆盖drawInContex:法:我在drawInContext中得到了无效的上下文错误

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSetLineWidth(context,5.0f); 
CGContextSetStrokeColorWithColor(context,[UIColor yellowColor].CGColor); 
CGRect newRec = CGRectMake(55,55,55,55); 
CGContextAddRect(context,newRec); 
CGContextDrawPath(context,kCGPathStroke); 

而且我用setNeedsDisplay中的touchesBegan:withEvent使drawInContext:可以调用。

它利用上下文中的每个逻辑(如CGContextSetLineWidth(背景下,5.0F);)获得像一个空值杆上的警告代码:

Error:CGContextSetWidth: invalid context 0x0 

所以,不是setNeedsDisplay创建我需要的情况下?

回答

1

您应该绘制到作为参数传递给drawInContext:方法的上下文。没有必要拨打UIGraphicsGetCurrentContext()

+0

我懂了...非常感谢。 – shashoukou 2011-04-13 09:52:23

+0

然后,您应该将此答案标记为已接受(单击复选标记)。 – 2011-04-13 12:40:52

相关问题