views:

38

answers:

1

I am currently using the following code to write a CGContext to a PNG on disk:

CGImageRef image = CGBitmapContextCreateImage(context);
UIImage *myImage = [UIImage imageWithCGImage:image];
NSData *data = UIImagePNGRepresentation(myImage);
[data writeToFile:path atomically:YES];

I found that the orientation of the resultant file is flipped from the way it appears on the screen(on an iPhone). What is the best way to flip this image on save so that when I later load the image it will be up right?

+2  A: 

The coordinate systems of UIImage and CG contexts are flipped from eachother. You can flip a CGContext by doing this before drawing into it:

CGContextTranslateCTM(context, 0, height);
CGContextScaleCTM(context, 1.0, -1.0);        
quixoto
perfect! Thanks.
toby