Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Image get rotated 90 degree after saved as PNG representation data

I have researched enough to get this working but not able to fix it. After taking picture from camera as long as I have image stored as UIImage, it's fine but as soon as I stored this image as PNG representation, its get rotated 90 degree.

Following is my code and all things I tried:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info  {     NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType];      if([mediaType isEqualToString:(NSString*)kUTTypeImage])      {         AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];         delegate.originalPhoto  = [info objectForKey:@"UIImagePickerControllerOriginalImage"];         NSLog(@"Saving photo");         [self saveImage];         NSLog(@"Fixing orientation");         delegate.fixOrientationPhoto  = [self fixOrientation:[UIImage imageWithContentsOfFile:[delegate filePath:imageName]]];               NSLog(@"Scaling photo");         delegate.scaledAndRotatedPhoto  =  [self scaleAndRotateImage:[UIImage imageWithContentsOfFile:[delegate filePath:imageName]]];     }     [picker dismissModalViewControllerAnimated:YES];     [picker release]; }   - (void)saveImage {     AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];     NSData *imageData = UIImagePNGRepresentation(delegate.originalPhoto);     [imageData writeToFile:[delegate filePath:imageName] atomically:YES]; } 

Here fixOrientation and scaleAndRotateImage functions taken from here and here respectively. They works fine and rotate image when I apply them on UIImage but doesn't work if I save image as PNG representation and apply them.

Please refere the following picture after executing above functions:

The first photo is original, second is saved, and third and fourth after applying fixorientation and scaleandrotate functions on saved image

like image 377
AlienMonkeyCoder Avatar asked Jun 01 '12 12:06

AlienMonkeyCoder


People also ask

Why are my pictures importing sideways?

The reason your photo would appear this way is because the photo was taken that way (either with the phone sideways or upside down) and the image file itself is in this orientation. For example, if you hold your phone upright and take a photo, the photo is saved in portrait mode or "sideways".

How do you fix a digital picture that is sideways?

Right-click the image and select Details to reveal a screen with metadata, including EXIF data, that you can adjust if the image supports it. Force a preferred orientation. Rotate the image, then save it.


2 Answers

Starting with iOS 4.0 when the camera takes a photo it does not rotate it before saving, it

simply sets a rotation flag in the EXIF data of the JPEG.If you save a UIImage as a JPEG, it

will set the rotation flag.PNGs do not support a rotation flag, so if you save a UIImage as a

PNG, it will be rotated incorrectly and not have a flag set to fix it. So if you want PNG

images you must rotate them yourself, for that check this link.

like image 109
Mudit Bajpai Avatar answered Nov 04 '22 17:11

Mudit Bajpai


Swift 3.1 version of the UIImage extension posted by Rao:

extension UIImage {     func fixOrientation() -> UIImage {         if self.imageOrientation == UIImageOrientation.up {             return self         }         UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)         self.draw(in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))         if let normalizedImage: UIImage = UIGraphicsGetImageFromCurrentImageContext() {             UIGraphicsEndImageContext()             return normalizedImage         } else {             return self         }     } } 

Usage:

let cameraImage = //image captured from camera let orientationFixedImage = cameraImage.fixOrientation() 

Swift 4/5:

UIImageOrientation.up has been renamed to UIImage.Orientation.up

like image 44
Andreas Avatar answered Nov 04 '22 18:11

Andreas