PDA

View Full Version : CGContext error




kevinrichardsuk
Jul 7, 2008, 06:31 AM
Hi everyone,

I'm trying to draw a PNG file to my iPhone screen and, although I've managed, the results are a little confusing..

Here is some code;


- (void)drawRect:(CGRect)rect {

[self reDraw];
}

- (void)reDraw {

CGContextRef cgContext = UIGraphicsGetCurrentContext();

CGImageRef imageRef = [[UIImage imageNamed:[self getImgFilename] ] CGImage];
CGRect frame = [self bounds];

CGContextDrawImage(cgContext, frame, imageRef);

NSLog([self getImgFilename]);
}

- (NSMutableString *)getImgFilename {

NSMutableString *newImage;

if (state=1)
{
newImage = [NSMutableString stringWithString:@"image1.png"];
}
else if (state=2)
{
newImage = [NSMutableString stringWithString:@"image2.png"];
}

return newImage;
}



So far, so basic.

The problem I have is that I get "<Error>: CGContextDrawImage: invalid context" whenever I try and call reDraw a second time.

The image is displayed when the app starts, if I call the reDraw function again then I get the error.

My other issue is that the image is flipped and mirrored, but that looks easily fixable compared to the error message.

Any ideas?? Thanks, Kevin



robbieduncan
Jul 7, 2008, 07:24 AM
You should move all the code from reDraw into drawRect:

I imagine you've done this as the documentation says something like "drawRect: should not be called directly". This does not mean that you should create a new method and call that: the reason that you should not call drawRect: directly is that it won't work for the same reason that your reDraw won't work.

When drawRect: gets called by the system it does a lot of work in advance including setting up the context for drawing. You have not done that which is why your context is invalid.

The correct way to get a view to redraw is to tell it that it needs redrawing via the setNeedsDisplay: method. This will cause the system to call drawRect: with a valid context.

kevinrichardsuk
Jul 7, 2008, 08:01 AM
ok - thanks for that. I now understand why it doesn't work - so the error doesn't appear at startup (and it consequently works) because drawRect is called by the system and manages the context.

I tried what you suggested but my UIView class doesn't recognise the setNeedsDisplay method, and any attempt to call it bombs the emulator..

robbieduncan
Jul 7, 2008, 08:05 AM
ok - thanks for that. I now understand why it doesn't work - so the error doesn't appear at startup (and it consequently works) because drawRect is called by the system and manages the context.

I tried what you suggested but my UIView class doesn't recognise the setNeedsDisplay method, and any attempt to call it bombs the emulator..

setNeedsDisplay: is an NSView method. I assume there is a similar UIView method. Note the :. That's important. It's called something like:


[myView setNeedsDisplay:YES];


That's for an NSView. A quick Google search indicates that for a UIView it doesn't take the argument. Check the documentation...

kevinrichardsuk
Jul 7, 2008, 08:10 AM
You beat me to it, I found that removing the :YES argument fixes the problem.

Thanks again