2013-05-30 26 views
0

在我的主視圖中,我有一個用戶定義的類drawSquare,它是UIImageView的子類。在drawSquare.m我具有以下所示的正常的drawRect方法:如何在我的主視圖中的UIImageView上繪製形狀?無效的上下文錯誤?

- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetLineWidth(context, 2.0); 
    CGContextSetStrokeColorWithColor(context,[UIColor redColor].CGColor); 
    redSquare = CGRectMake(2,2,30,34); 
    CGContextAddRect(context, redSquare); 
    CGContextStrokePath(context); 
} 

以我主視圖控制器我有一個名爲word1Area類型drawSquare作爲一個IBOutlet的對象。 在我的主視圖控制器我viewDidLoad方法我有臺詞:

[self.view addSubview:word1Area]; 
[word1Area drawRect:word1Area.frame]; 

我得到一個「無效的情況下」運行時錯誤。我嘗試過調用drawRect方法的其他變體,但不斷得到相同的錯誤。謝謝你的幫助。

+0

你見過這個答案http://stackoverflow.com/questions/13153223/how-to-crop-the-image-using-uibezierpath/15353848#15353848 –

回答

0

你不能自己調用​​drawRect - 你可以告訴視圖setNeedsDisplay

相關問題