2011-04-13 52 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

相關問題