CGContextDrawImage draws image upside down when passed UIImage.CGImage
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.
Even after applying everything I have mentioned, I've still had dramas with the images. In the end, i've just used Gimp to create a 'flipped vertical' version of all my images. Now I don't need to use any Transforms. Hopefully this won't cause further problems down the track.
Does anyone know why CGContextDrawImage would be drawing my image upside down? I am loading an image in from my application:
Quartz2d uses a different co-ordinate system, where the origin is in the lower left corner. So when Quartz draws pixel x[5], y[10] of a 100 * 100 image, that pixel is being drawn in the lower left corner instead of the upper left. Thus causing the 'flipped' image.
The x co-ordinate system matches, so you will need to flip the y co-ordinates.
CGContextTranslateCTM(context, 0, image.size.height);
This means we have translated the image by 0 units on the x axis and by the images height on the y axis. However, this alone will mean our image is still upside down, just being drawn "image.size.height" below where we wish it to be drawn.
The Quartz2D programming guide recommends using ScaleCTM and passing negative values to flip the image. You can use the following code to do this -
CGContextScaleCTM(context, 1.0, -1.0);
Combine the two just before your CGContextDrawImage
call and you should have the image drawn correctly.
UIImage *image = [UIImage imageNamed:@"testImage.png"];
CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
CGContextTranslateCTM(context, 0, image.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, imageRect, image.CGImage);
Just be careful if your imageRect co-ordinates do not match those of your image, as you can get unintended results.
To convert back the coordinates:
CGContextScaleCTM(context, 1.0, -1.0);
CGContextTranslateCTM(context, 0, -imageRect.size.height);