ioscocoa-touchcore-graphicscgcontextdrawimage

CGContextDrawImage draws image upside down when passed UIImage.CGImage


Does anyone know why CGContextDrawImage would be drawing my image upside down? I am loading an image in from my application:

UIImage *image = [UIImage imageNamed:@"testImage.png"];

And then simply asking core graphics to draw it to my context:

CGContextDrawImage(context, CGRectMake(0, 0, 145, 15), image.CGImage);

It renders in the right place, and dimensions, but the image is upside down. I must be missing something really obvious here?


Solution

  • Instead of

    CGContextDrawImage(context, CGRectMake(0, 0, 145, 15), image.CGImage);
    

    Use

    [image drawInRect:CGRectMake(0, 0, 145, 15)];
    

    In the middle of your begin/end CGcontext methods.

    This will draw the image with the correct orientation into your current image context - I'm pretty sure this has something to do with the UIImage holding onto knowledge of the orientation while the CGContextDrawImage method gets the underlying raw image data with no understanding of orientation.