【发布时间】:2011-12-11 22:16:13
【问题描述】:
我正在使用以下代码来保存和加载我从库中选择或使用相机拍摄的图像:
//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 中显示的方式:
imgView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
但是,当我选择图像并将其设置为在我的UIImageView 中显示时,这很好,但是当我加载该图像时,它通常是错误的方向。有任何想法吗?有没有人遇到过这种情况或知道我该如何解决?
谢谢。
编辑:
看来,如果您加载一张以纵向倒置拍摄的照片,它会以该方向加载,如果您以横向拍摄一张照片,它会以该方向加载。任何想法如何解决这个问题?无论加载什么方向,它们总是以 UIImageOrientationUp 的形式返回。
【问题讨论】:
-
只需查看 UIImageOrientationUp 以获取 UIImage 文档。你可以设置 UIImage 对象的 imageOrientation 属性。
-
我怎样才能应用这个方向呢?我看了一下,似乎只是为了比较,轮换必须手动完成。
-
您可以这样解决这个问题:[修复图像方向][1] [1]: stackoverflow.com/questions/13393909/…
标签: iphone objective-c ipad uiimageview orientation