iphone - iOS : Image get rotated 90 degree after saved as PNG representation data

标签 iphone uiimage image-rotation ios5.1 landscape-portrait

我已经进行了足够的研究来使其正常工作,但无法修复它。从相机拍照后,只要我将图像存储为 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函数取自herehere分别。当我将它们应用到 UIImage 上时,它们工作正常并旋转图像,但如果我将图像保存为 PNG 表示形式并应用它们,它们就不起作用。

执行以上功能后,请引用下图:

The first photo is original, second is saved, and third and fourth after applying fixorientation and scaleandrotate functions on saved image

最佳答案

从 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/

相关文章:

iphone - iToast从不消失在我的iPhone应用程序中

iphone - UIPickerView:旋转时获取行值?

ios - 更改 UITextView 键盘返回按钮的正确方法是什么

ios - 设置 CIFiltered UIImage 时 UIImageVIew 崩溃

objective-c - 在 Objective C 中通过 POST 上传图片

iPhone:MKMapView 奇怪的崩溃

ios - 如何删除通过代码创建的 UIImageView?

html - Kinetic js 拖放、调整大小和旋转图像

python - OpenCV python3无法正确旋转图像

html - 如何在不旋转图像CSS的情况下以椭圆形式旋转图像