Iphone 保存到磁盘时翻转图像

Iphone 保存到磁盘时翻转图像,iphone,cocoa-touch,uikit,core-graphics,Iphone,Cocoa Touch,Uikit,Core Graphics,我目前正在使用以下代码将CGContext写入磁盘上的PNG: CGImageRef image = CGBitmapContextCreateImage(context); UIImage *myImage = [UIImage imageWithCGImage:image]; NSData *data = UIImagePNGRepresentation(myImage); [data writeToFile:path atomically:YES]; 我发现结果文件的方向与它在屏幕上的显

我目前正在使用以下代码将CGContext写入磁盘上的PNG:

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

我发现结果文件的方向与它在屏幕上的显示方式不同(在iPhone上)。保存时翻转此图像的最佳方式是什么,以便我以后加载图像时它会正确地向上?

UIImage和CG上下文的坐标系会相互翻转。在绘制CGContext之前,可以通过执行以下操作来翻转CGContext:

CGContextTranslateCTM(context, 0, height);
CGContextScaleCTM(context, 1.0, -1.0);        

UIImage和CG上下文的坐标系相互翻转。在绘制CGContext之前,可以通过执行以下操作来翻转CGContext:

CGContextTranslateCTM(context, 0, height);
CGContextScaleCTM(context, 1.0, -1.0);