1

The image comes from UIImagePickerViewController:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        print("1: \(image)")
        if let data = UIImagePNGRepresentation(image) {
            print("2: \(UIImage(data: data))")
        }
    }
}

The result is following:

1: size {3024, 4032} orientation 3 scale 1.000000
2: Optional( size {4032, 3024} orientation 0 scale 1.000000)

Why UIImagePNGRepresentation() changes orientation of the image? How to prevent from this?

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
  • check [this post](http://stackoverflow.com/questions/15956750/avfoundation-image-orientation-off-by-90-degrees-in-the-preview-but-fine-in-came/16074603#16074603) which prevents rotation. Code is in objective-c you can use it as is or convert it in swift. – Dipen Panchasara Dec 09 '15 at 10:09
  • It is too complicated, for sure there is much simpler way. – Bartłomiej Semańczyk Dec 09 '15 at 10:15
  • you just have to pass your image to function and it takes care of rest, though i have added swift code as well. check [now](http://stackoverflow.com/questions/15956750/avfoundation-image-orientation-off-by-90-degrees-in-the-preview-but-fine-in-came/16074603#16074603) – Dipen Panchasara Dec 09 '15 at 10:16

1 Answers1

0

In first case your image orientation is UIImageOrientationRight (value 3). In second case the orientation is UIImageOrientationUp value 0 which is default.

You can fix the orientation issues while converting images to data and vice versa.

Please se this answer iOS UIImagePickerController result image orientation after upload

Update: You can change fix its orientation with code in above link. It will not affect the uploading procedure.

If you are not bothered about the data format, you can convert it to JPEG format

like if let data = UIImageJPEGRepresentation(image, 1.0) {
  print("2: \(UIImage(data: data))")
}
Community
  • 1
  • 1
Johnykutty
  • 12,091
  • 13
  • 59
  • 100