0

When I take picture in Portrait Mode, savedImage is automatically rotated when setting to UIImageView.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"ProfilePicture.png"];
[UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES];

UIImage *savedImage = [UIImage imageWithData:[NSData dataWithContentsOfFile:filePath]];

But when I use UIImageJPEGRepresentation: writeToFile: atomically: method, the result Image has No Issues.

Can anyone please explain why I didn't get image in correct orientation while using UIImageJPEGRepresentation & not in UIImagePNGRepresentation.

NB: I tried

UIImage *imageToDisplay =
     [UIImage imageWithCGImage:[originalImage CGImage]
              scale:[originalImage scale]
              orientation: UIImageOrientationUp];

to save imageToDisplay, but not working

Lal Krishna
  • 15,485
  • 6
  • 64
  • 84

1 Answers1

1

Its because of the rotation flag in EXIF data of the JPEG. every image file has metadata property. If the metadata specifies the orientation of the image which is generally ignored by other OS but Mac. Most of images taken are having their meta data property set to right angle. So Mac shows it 90 degree rotated manner. You can see the same image in proper way in windows OS. please check this answer which shows more details

Amal T S
  • 3,327
  • 2
  • 24
  • 57