CGContextFillEllipseInRect:无效的上下文

时间:2010-05-23 10:22:08

标签: iphone

当我启动我的应用程序时,我不断收到此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)什么是无效的上下文错误?

谢谢你们。

1 个答案:

答案 0 :(得分:0)

  1. 我不确定这六个调用,但至少有两个调用正在发生,因为框架正在调用drawRect:,你也是。您无需亲自致电drawRect:(因此您无需覆盖setNeedsDisplay:)。
  2. 您应该从UIGraphicsGetCurrentContext内拨打drawRect:。不要缓存它。它可能会失败,因为框架不保证在每次调用时使用相同的上下文,或者因为您直接调用drawRect:,或两者兼而有之;我不确定是哪一个。