Image orientation issues

Hi


First: Appollogies, I also posted this in the AVFoundation (video and camera) forum but then I noticed there is very little activity there and even fewer responses...


I'm using Swift 4 and the AvCam example code from Apple. In my own app I've an issue where the captured image orientation is wrong. My requirement is to save the photos to my applications folder as opposed to the Photos library. I've spent a day trying to correct my own code and got nowhere.


As a test I modified the AvCam code to write the photo data to a static variable in a struct and then display that image on the camera view in AvCam by tapping a button as follows:


// In the camera view controller:


@IBOutlet weak var pv: UIImageView! // for previewing test


@IBAction func btn(_ sender: UIButton) { // for setting preview test image

let dataProvider = CGDataProvider(data: photoTest.photo! as CFData)

let cgImageRef: CGImage! = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: .defaultIntent)

let photo = UIImage(cgImage: cgImageRef)

pv.contentMode = .scaleAspectFill

pv.image = photo


}


The orientaion issue is replicated in AvCam with the above code. In my own app the image is previewed before saving to the apps folder and the images orientation is always wrong in the preview as well as when reloading the image from disk. I.e. if the iphone is in portrait orientation, the preview image is rotated 90 degrees anti-clockwise, in landscape the same happens so the preview image appears to be the correct way up.


I'm assuming I've completely missed the point somewhere along the line as it seems to me that regardless of device orientation, the top of a photo should always be the top. For example, in the Photos app photos are shown the correct way up regardless of orientation.


Any help would be much appreciated, this is driving me nuts ;o)


Cheers

Replies

Cause of the problem :

h ttps://stackoverflow.com/questions/13074161/uiimage-from-uiimagepickercontroller-orientation-issue


I read through this forum and think you will find the solution (maybe in objc, but that should be easy to port to Swift).

h ttps://github.com/BradLarson/GPUImage/issues/895 Look for zenvendof commented on 21 Apr 2014

Hi Claude

thanks very much, I already fixed this (thanks to bford in the AVFoundation forum, see https://forums.developer.apple.com/thread/102884. I should have put the answer here too but forgot sorry.

Basically I’d based my camera on a (bad) example that I found on the net. This example has preview before saving functionality (which I do find annoying and dropped after).

The problem was, as described in the posts you referenced, that I was converting the image data to UIImage (see my original post), the conversion wasn’t retaining the orientation. Also note that the image pickers camera wasn’t much use as I wanted to store location and heading data. The image picker strips this data, though given an asset url you can in fact gets geo data if it exists.

The solution was so simple it went straight over my head for a couple of hours as I was still providing a preview before save. I.e. I was saving the preview image with bad orientation. I should have saved the image first.

Assuming the camera is properly set up, see AvCam for a great example, all you need to do is save the photo Data’s fileDataRepresentation to disk and then load that saved image and the orientation is handled perfectly.

Also note, I wasn’t following the AvCam example completely as the sample code saves to the Photos library and I wanted to save to the application folder.

Thanks again

Mark