UIImagePNGRepresentation问题? /图像旋转90度 [英] UIImagePNGRepresentation issues? / Images rotated by 90 degrees
问题描述
我想从UIImagePickerController加载图片,然后将所选照片保存到我应用的文档目录。
I want to load images from UIImagePickerController, then save the selected photo to my app's document directory.
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *data1 = UIImagePNGRepresentation(image);
NSString *fileName = "1.png";
NSString *path = //get Document path, then add fileName
BOOL succ = [data1 writeToFile:path atomically:YES];
但是我将图像保存到我的文档后,我发现图像旋转了90度,然后我将方法UIImagePNGRepresentation改为UIImageJPEGRepresentation,这一次很好,任何人知道是什么问题?
but after I save the image to my document, I found that, the image was rotated 90 degree, then I change the method UIImagePNGRepresentation to UIImageJPEGRepresentation, this time it's fine, anyone know what's the problem?
推荐答案
只是想出了原因:从iOS 4.0开始,当相机拍摄照片,它不保存前旋转它,它只是在JPEG的EXIF数据中设置一个旋转标志。
I had the same problem and just figured out the reason: starting with iOS 4.0 when the camera takes a photo it does not rotate it before saving, it simply sets a rotation flag in the EXIF data of the JPEG.
如果将UIImage保存为JPEG,它将设置旋转标志。
If you save a UIImage as a JPEG, it will set the rotation flag.
PNG不支持旋转标志,因此如果将UIImage保存为一个PNG,它会旋转不正确,没有设置标志来修复它。所以如果你想要PNG,你必须自己旋转它们。
PNGs do not support a rotation flag, so if you save a UIImage as a PNG, it will be rotated incorrectly and not have a flag set to fix it. So if you want PNGs you must rotate them yourself.
我会称之为PNG保存功能的一个bug,但这只是一个意见(他们应该至少警告你这)。
I would call this a bug in the PNG saving function but that's just an opinion (they should at least warn you about this).
这篇关于UIImagePNGRepresentation问题? /图像旋转90度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!