Save UIImage, Load it in Wrong Orientation
Solution 1
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
is what we need.
Thanks,
Solution 2
The root cause is PNG format doesn't have image orientation information, but JPEG format does. So the easiest way to solve this problem is saving your file in JPEG format using UIImageJPEGRepresentation()
Solution 3
Check the EXIF information for the image that you're loading, there should be a tag for orientation. Use the value to rotate your image to proper orientation.
This website should get you started.
http://www.impulseadventure.com/photo/exif-orientation.html
(The application you used to view the photo must have this built in, that's why you see correct orientation when opening the photo)
Josh Kahane
Updated on June 02, 2022Comments
-
Josh Kahane almost 2 years
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]; }
This is how I set the picked image to be shown in my
UIImageView
:imgView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
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?Thanks.
EDIT:
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.
-
Josh Kahane over 12 yearsAhh very interesting, however how would i go about fetching that exif orientation data?
-
zakdances almost 12 yearsDoes this work even after the UIImage has been converted to NSData and back again?
-
Rick van der Linde over 10 yearsThank you, was having trouble with weird rotations for 6 hours when I finally found this answer.
-
Scott Allen over 10 yearsThis resolved my issue, but only once I started using the ALAsset orientation attribute instead of the UIImage orientation attribute.
-
micnguyen over 9 yearsThis has solved my issue with my saved images not returning in the proper orientation - especially those taken from the Camera. The only drawback from using the JPEG alternative is that transparency is not retained, correct?
-
Gang Fang about 5 yearsAlso need to be cautious that with JPEG you get lossy images
-
DawnSong almost 4 yearsRotate before saving to disk like this
-
DawnSong almost 4 yearsSo rotating it like this is necessary.