Jos*_*ane 39 iphone objective-c orientation uiimageview ipad
我使用以下代码来保存和加载我从库中选择的图像或使用相机:
//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];
}
Run Code Online (Sandbox Code Playgroud)
这就是我将拾取的图像设置为在我的显示中的方式UIImageView
:
imgView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
Run Code Online (Sandbox Code Playgroud)
但是,当我选择和图像并将其设置为在我显示时UIImageView
它很好,但是当我加载该图像时,它通常是错误的方向.有任何想法吗?有没有人经历过这个或知道如何解决这个问题?
谢谢.
编辑:
所以看起来,如果你加载的照片是以倒置的肖像拍摄的,那么它就会以那个方向加载,如果你在横向拍摄照片,它就会以那个方向加载.任何想法如何解决这个问题?无论它们加载什么方向,它们总是以UIImageOrientationUp的形式返回.
Rav*_*vin 28
我遇到了类似的问题,这就是我如何解决它.
在我们保存图像的同时,我们需要将其方向信息与图像一起保存 ...
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setInteger:[myImage imageOrientation] forKey:@"kImageOrientation"];
[imageOrientation release];
Run Code Online (Sandbox Code Playgroud)
我们加载图像,我们需要读取它的方向信息并将其应用于图像......
UIImage *tempImage = [[UIImage alloc] initWithContentsOfFile:fullPath];
UIImage *orientedImage= [[UIImage alloc] initWithCGImage: tempImage.CGImage scale:1.0 orientation:imageOrientation];
[tempImage release];
Run Code Online (Sandbox Code Playgroud)
orientedImage
是我们需要的.
谢谢,
归档时间: |
|
查看次数: |
17723 次 |
最近记录: |