tags:

views:

56

answers:

1

Hi all, when I launch my app, I keep getting this CGContextFillEllipseInRect: invalid context error. My app is simply to make the circle 'pinchable'.

The debugger shows me this:

[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

My implementation file is:

//
//  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

I've got two questions: 1) Why is it updating counter 6 times? I removed the line [super setNeedsDisplay]; but it becomes 4 times. 2) What is that invalid context error?

Thanks guys.

A: 
  1. I'm not sure about the six calls, but at least two calls are happening because the framework is calling drawRect: and so are you. You don't need to call drawRect: yourself (and hence you don't need to override setNeedsDisplay:).
  2. You should call UIGraphicsGetCurrentContext from inside drawRect:. Don't cache it. It may be failing because the framework doesn't guarantee to use the same context on each call, or because you are calling drawRect: directly, or both; I'm not sure which.
Marcelo Cantos
hakewake
The huge oval is due to the second call (`counter == 1`), which is then invoking the second ellipse code and using the `rect` passed by the framework. It's basically advising you to draw the entire screen. Do you ever call `setNeedsDisplay:` or `setNeedsDisplayInRect:`? This may account for the two calls.
Marcelo Cantos