从Filemanager加载时,UIImage会自动旋转

时间:2017-07-28 12:07:16

标签: ios objective-c uiimageview uiimage

当我在纵向模式中拍照时,savedImage会在设置为UIImageView时自动旋转。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"ProfilePicture.png"];
[UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES];

UIImage *savedImage = [UIImage imageWithData:[NSData dataWithContentsOfFile:filePath]];

但是当我使用UIImageJPEGRepresentation: writeToFile: atomically:方法时,结果Image没有问题。

任何人都可以解释为什么我在使用UIImageJPEGRepresentation&不在UIImagePNGRepresentation

注意:我试过了

UIImage *imageToDisplay =
     [UIImage imageWithCGImage:[originalImage CGImage]
              scale:[originalImage scale]
              orientation: UIImageOrientationUp];

保存imageToDisplay,但无法正常工作

1 个答案:

答案 0 :(得分:1)

它是因为JPEG的EXIF数据中的旋转标志。每个图像文件都有元数据属性。如果元数据指定图像的方向,除了Mac之外,其他操作系统通常会忽略该方向。拍摄的大多数图像都将其元数据属性设置为直角。所以Mac显示它旋转90度的方式。您可以在Windows操作系统中以适当的方式查看相同的图像。请查看this回答,其中显示了更多详细信息