2010-05-23 63 views
0

當我啓動我的應用程序,我不斷收到這個CGContextFillEllipseInRect:無效的上下文錯誤。我的應用程序只是爲了讓這個圈子變得「可捏造」。CGContextFillEllipseInRect:無效的上下文

調試器顯示我:

[Session started at 2010-05-23 18:21:25 +0800.] 
2010-05-23 18:21:27.140 Erase[8045:207] I'm being redrawn. 
Sun May 23 18:21:27 Sidwyn-Kohs-MacBook-Pro.local Erase[8045] <Error>: CGContextFillEllipseInRect: invalid context 
2010-05-23 18:21:27.144 Erase[8045:207] New value of counter is 1 
2010-05-23 18:21:27.144 Erase[8045:207] I'm being redrawn. 
Sun May 23 18:21:27 Sidwyn-Kohs-MacBook-Pro.local Erase[8045] <Error>: CGContextFillEllipseInRect: invalid context 
2010-05-23 18:21:27.144 Erase[8045:207] New value of counter is 2 
2010-05-23 18:21:27.144 Erase[8045:207] I'm being redrawn. 
Sun May 23 18:21:27 Sidwyn-Kohs-MacBook-Pro.local Erase[8045] <Error>: CGContextFillEllipseInRect: invalid context 
2010-05-23 18:21:27.144 Erase[8045:207] New value of counter is 3 
2010-05-23 18:21:27.145 Erase[8045:207] I'm being redrawn. 
Sun May 23 18:21:27 Sidwyn-Kohs-MacBook-Pro.local Erase[8045] <Error>: CGContextFillEllipseInRect: invalid context 
2010-05-23 18:21:27.145 Erase[8045:207] New value of counter is 4 
2010-05-23 18:21:27.148 Erase[8045:207] I'm being redrawn. 
Sun May 23 18:21:27 Sidwyn-Kohs-MacBook-Pro.local Erase[8045] <Error>: CGContextFillEllipseInRect: invalid context 
2010-05-23 18:21:27.149 Erase[8045:207] New value of counter is 5 
2010-05-23 18:21:27.150 Erase[8045:207] I'm being redrawn. 
Sun May 23 18:21:27 Sidwyn-Kohs-MacBook-Pro.local Erase[8045] <Error>: CGContextFillEllipseInRect: invalid context 
2010-05-23 18:21:27.150 Erase[8045:207] New value of counter is 6 

我的執行文件是:

// 
// ImageView.m 
// Erase 
// 

#import "ImageView.h" 
#import "EraseViewController.h" 

@implementation ImageView 


-(void)setNewRect:(CGRect)anotherRect{ 
    newRect = anotherRect; 
} 

- (id)initWithFrame:(CGRect)frame { 
    if (self = [super initWithFrame:frame]) { 
     newRect = CGRectMake(0,0,0,0); 
     ref = UIGraphicsGetCurrentContext(); 
    } 
    return self; 
} 



- (void)drawRect:(CGRect)rect { 
    static int counter = 0; 
    CGRect veryNewRect = CGRectMake(30.0, 210.0, 60.0, 60.0); 
    NSLog(@"I'm being redrawn."); 

    if (counter == 0){ 
     CGContextFillEllipseInRect(ref, veryNewRect); 
    } 
    else{ 
     CGContextFillEllipseInRect(ref, rect); 
    } 

    counter++; 
    NSLog(@"New value of counter is %d", counter); 
} 

- (void)setNeedsDisplay{ 
    [super setNeedsDisplay]; 
    [self drawRect:newRect]; 
} 

- (void)dealloc { 
    [super dealloc]; 
} 


@end 

我有兩個問題: 1)爲什麼更新計數器6倍?我刪除了行[super setNeedsDisplay];,但它變成了4次。 2)什麼是無效的上下文錯誤?

謝謝你們。

回答

0
  1. 我不確定這六個調用,但至少有兩個調用正在發生,因爲框架調用drawRect:,你也是如此。你不需要自己撥打drawRect:(因此你不需要覆蓋setNeedsDisplay:)。
  2. 您應該從drawRect:內撥打UIGraphicsGetCurrentContext。不要緩存它。它可能會失敗,因爲框架不能保證在每次調用時使用相同的上下文,或者因爲您直接調用了drawRect:,或者兩者都調用;我不確定哪個。
+0

嗨馬塞洛,謝謝你的回覆。 剛纔你說的1)刪除重寫的setNeedsDisplay 2)把CGContextRef上下文放在drawRect:方法中。 但是,我仍然收到2個電話,當我啓動應用程序時,會有一個適合屏幕寬度和高度的巨大橢圓形。 有什麼想法? – 2010-05-23 10:33:14

+0

巨大的橢圓是由於第二次調用('counter == 1'),然後調用第二個橢圓代碼並使用框架傳遞的'rect'。基本上建議您繪製整個屏幕。你曾經調用過'setNeedsDisplay:'或'setNeedsDisplayInRect:'嗎?這可能解釋了這兩個電話。 – 2010-05-23 10:58:19

相關問題