我已经进行了足够的研究来使其正常工作,但无法修复它。从相机拍照后,只要我将图像存储为 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 and here分别。当我将它们应用到 UIImage 上时,它们工作正常并旋转图像,但如果我将图像保存为 PNG 表示形式并应用它们,它们就不起作用。
执行以上功能后,请参考下图:
从 iOS 4.0 开始,相机拍照时不会在保存前旋转照片,
只需在 JPEG 的 EXIF 数据中设置一个旋转标志。如果将 UIImage 保存为 JPEG,它
将设置旋转标志。PNG 不支持旋转标志,因此如果将 UIImage 保存为
PNG,它将被错误地旋转,并且没有设置标志来修复它。所以如果你想要PNG
您必须自己旋转图像,为此请检查此link.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)