我已经进行了足够的研究来使其正常工作,但无法修复它。从相机拍照后,只要我将图像存储为 UIImage,就可以了,但是一旦我将此图像存储为 PNG 表示形式,它就会旋转 90 度。
以下是我的代码和我尝试过的所有内容:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType];
if([mediaType isEqualToString:(NSString*)kUTTypeImage])
{
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
delegate.originalPhoto = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSLog(@"Saving photo");
[self saveImage];
NSLog(@"Fixing orientation");
delegate.fixOrientationPhoto = [self fixOrientation:[UIImage imageWithContentsOfFile:[delegate filePath:imageName]]];
NSLog(@"Scaling photo");
delegate.scaledAndRotatedPhoto = [self scaleAndRotateImage:[UIImage imageWithContentsOfFile:[delegate filePath:imageName]]];
}
[picker dismissModalViewControllerAnimated:YES];
[picker release];
}
- (void)saveImage
{
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSData *imageData = UIImagePNGRepresentation(delegate.originalPhoto);
[imageData writeToFile:[delegate filePath:imageName] atomically:YES];
}
这里的fixOrientation和scaleAndRotateImage函数取自here和 here分别。当我将它们应用到 UIImage 上时,它们工作正常并旋转图像,但如果我将图像保存为 PNG 表示形式并应用它们,它们就不起作用。
执行以上功能后,请引用下图:
最佳答案
从 iOS 4.0 开始,当相机拍照时,它在保存之前不会旋转它,
只需在 JPEG 的 EXIF 数据中设置一个旋转标志。如果将 UIImage 保存为 JPEG,则它
将设置旋转标志。PNG不支持旋转标志,因此如果将 UIImage 保存为
PNG,它将被错误地旋转,并且没有设置标志来修复它。所以如果你想要 PNG
您必须自己旋转图像,为此请检查此 link .
关于iphone - iOS : Image get rotated 90 degree after saved as PNG representation data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10850184/