我使用以下代码保存和加载从库中拾取或使用相机拍摄的图像:
//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
中时,这是可以的,但当我加载该图像时,它通常是错误的方向.有什么 idea 吗?有没有人经历过,或者知道我如何解决这个问题?
谢谢
编辑:
所以看起来,如果你加载一张照片,它是一张倒挂在肖像上的照片,它加载在那个方向上,如果你在横向拍摄一张照片,它加载在那个方向上.有什么办法吗?无论加载的方向是什么,它们总是以UIImageOrientationUp返回.