我使用下面的代碼保存和載入,我無論從庫中挑選圖片或使用相機拍攝:保存的UIImage,加載它在錯誤的方向
//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的形式返回。
只需查看UIImage文檔的UIImageOrientationUp即可。你可以設置UIImage對象的imageOrientation屬性。 –
但我怎樣才能應用這個方向?我看了一下,它似乎只是爲了比較,而且輪換必須手動完成。 –
請檢查我的編輯。 –