Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

kevinrichardsuk

macrumors member
Original poster
May 19, 2008
30
0
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;

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

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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

macrumors member
Original poster
May 19, 2008
30
0
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

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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:

Code:
[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...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.