Save UIImage, Load it in Wrong Orientation(保存 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,以错误的方向加载它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:保存 UIImage,以错误的方向加载它
基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01