CGContext error

Discussion in 'iOS Programming' started by kevinrichardsuk, Jul 7, 2008.

  1. macrumors member

    Joined:
    May 19, 2008
    #1
    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
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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.
     
  3. thread starter macrumors member

    Joined:
    May 19, 2008
    #3
    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..
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    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...
     
  5. thread starter macrumors member

    Joined:
    May 19, 2008
    #5
    You beat me to it, I found that removing the :YES argument fixes the problem.

    Thanks again
     

Share This Page