2010-05-23 29 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

相关问题