0

I have Swift code that uses the UIImagePickerController to have the user select a photo, a CIFilter is applied, then that UIImage is applied to a UIImageView. The problem is that I have no idea how make that image be in the correct orientation shown in the UIImagePickerController or in the Photos app. It's independent of the UIImage's imageOrientation. For instance, two different photos imported with a imageOrientation of "0" that are both portrait can resolve as a new UIImage with two completely different orientations. Maybe this is determined by EXIF information? Maybe it's both? That's why it's confusing.

Ultimately, I just want to have this image imported in the orientation shown in the UIImagePickerController or Photos app. If they can do it, there's some way I can.

@objc func imagePickerController(_ picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!) {

    let selectedImage : UIImage = image

    print("selectedImage.imageOrientation = \(selectedImage.imageOrientation.rawValue)")

    let isPortrait:Bool = image.size.height > image.size.width ? true : false

    dismiss(animated: true, completion: { () in
        DispatchQueue.main.async {
            self.enableButtons(enabled:false)
            self.activityTextStatus?.setText("Preparing", center:self.view.center, addToView:self.loadingContainer, setRotation:true)
        }

        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
            self.exportImagesToPreview(capturedImage: selectedImage,
                                       orientation:nil,
                                       isPortrait: isPortrait)
             // here I apply an effect to the image,
             // and it may be in the incorrect orientation

        }
    })

}
Chewie The Chorkie
  • 4,896
  • 9
  • 46
  • 90
  • 1
    `? true : false` is redundant – Leo Dabus Aug 17 '18 at 16:21
  • Leo, do you know any information on how to solve the problem that I'm asking about? – Chewie The Chorkie Aug 17 '18 at 16:43
  • 1
    Yes you need to flatten/ redraw your image – Leo Dabus Aug 17 '18 at 17:18
  • 1
    https://stackoverflow.com/questions/42098390/swift-png-image-being-saved-with-incorrect-orientation/42098812#42098812 – Leo Dabus Aug 17 '18 at 17:19
  • As I mentioned, I can have two different imported photos with the "up" orientation, but that doesn't mean they will result in the correct orientation. Therefor, importing photos cannot rely on imageOrientation. It probably needs to use meta information like Exif. – Chewie The Chorkie Aug 17 '18 at 18:06
  • 1
    if you redraw your image you don't need to worry about it – Leo Dabus Aug 17 '18 at 18:07
  • I see now it relies on "UIImagePNGRepresentation". I will look into that and see if that helps. – Chewie The Chorkie Aug 17 '18 at 18:07
  • it just creates a new image context. Make sure you do it only once after applying your filter because it is not a simple operation – Leo Dabus Aug 17 '18 at 18:10
  • Can I do it immediately after importing it instead of after applying the filter? Most of my filters depend on the orientation of the image to have the filter look right. – Chewie The Chorkie Aug 17 '18 at 19:44
  • 1
    No. The problem it is actually the filter itself. You can display your ciimage using an UIImageView but you can't get a data representation of it without redrawing it in a new image context – Leo Dabus Aug 17 '18 at 19:47
  • Oh my god you're the best! Just a note though, I'm doing it immediately after the import. It works with all the images I both did and didn't have issues with before. – Chewie The Chorkie Aug 17 '18 at 19:59
  • 1
    I think it is worth mentioning that UIImagePNGRepresentation discards the image orientation, therefore the need to create a new image context. If you use JPEG representation it saves the orientation but if you apply a filter you would need to create a new image context anyway before getting its data for persistence. – Leo Dabus Aug 17 '18 at 20:00

0 Answers0