保存UIImage,以错误的方向加载它 [英] Save UIImage, Load it in Wrong Orientation

查看:85
本文介绍了保存UIImage,以错误的方向加载它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码来保存和加载我从库中选择的图像或使用相机拍摄的图像:

I am using the following code to save and load images that I pick from either the library or take using the camera:

//saving an image
- (void)saveImage:(UIImage*)image:(NSString*)imageName {
    NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.
    NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it
    NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]]; //add our image to the path
    [fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image)
    NSLog(@"image saved");
}

//loading an image
- (UIImage*)loadImage:(NSString*)imageName {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]];
    return [UIImage imageWithContentsOfFile:fullPath];
}

这是我设置拾取图像的方式,以便在我的<$ c中显示$ c> UIImageView :

This is how I set the picked image to be shown in my UIImageView:

imgView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

然而,当我选择并成像并将其设置为显示在我的中UIImageView 它很好,但是当我加载该图像时,它通常是错误的方向。有任何想法吗?有没有人经历过这个或知道如何解决这个问题?

However, when I pick and image and set it to be shown in my UIImageView it is fine, but when I load that image it often is the wrong orientation. Any ideas? Anyone experienced this or know how I could resolve this?

谢谢。

编辑:

所以看来,如果你加载的照片是以倒置的肖像方式拍摄的,那么它会以该方向加载,如果你在横向拍摄照片,它会以该方向加载。任何想法如何解决这个问题?无论他们加载什么方向,他们总是返回UIImageOrientationUp。

So it seems, if you load a photo which was taken a photo in portrait upside-down, it loads in that orientation, if you take a photo in landscape left it loads in that orientation. Any ideas how to get around this? Whatever orientation they load it they always return as UIImageOrientationUp.

推荐答案

我遇到了类似的问题,这就是我如何解决它。

I have faced similar problem and here is how I solved it.

在我们保存图片时,我们需要 保存其方向信息 以及图片......

While we save image we need to save its orientation information along with the image ...

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setInteger:[myImage imageOrientation] forKey:@"kImageOrientation"];
[imageOrientation release];

我们需要加载图片 读取其方向信息并应用 到图像...

And we load image we need to read its orientation information and apply it to the image…

UIImage *tempImage = [[UIImage alloc] initWithContentsOfFile:fullPath];
UIImage *orientedImage= [[UIImage alloc] initWithCGImage: tempImage.CGImage scale:1.0 orientation:imageOrientation];
[tempImage release];

orientedImage 是我们需要的。

谢谢,

这篇关于保存UIImage,以错误的方向加载它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆