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

查看:17
本文介绍了保存 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];
}

这就是我将选取的图像设置为在我的 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天全站免登陆