0

My code takes a photo. Then saves it with a masking effect applied on it. The only problem is it is being saved at a -90 from the way I took it a portrait photo orientation. All I want to do is take a photo in portrait and have the photo be saved with out the rotation applied to it.

[Saved Pic when Mask is applied][2]

[Mask][3]

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

    let red = info[UIImagePickerControllerOriginalImage] as! UIImage
             self.dismiss(animated: true)
//Sugguested Code
    red.imageOrientation = UIDevice.current.orientation.imageOrientation
//


    self.currentImageView?.image = red

    let image = photo.image
       let maskingImage = UIImage(named: "mask")
    photo.image = maskImage(image: image!, mask: maskingImage!)
    photo.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleBottomMargin, .flexibleRightMargin, .flexibleLeftMargin, .flexibleTopMargin]
    photo.contentMode = .scaleAspectFit
    photo.clipsToBounds = true

  }
  • Do you need to take a image in portrait and then apply a mask on that then save it, but your result image is saving on landscape? – Fa.Shapouri May 04 '17 at 13:54
  • Yes that is exactly what is happening. –  May 04 '17 at 13:57
  • The orientation of the mask after saving image is true or not? – Fa.Shapouri May 04 '17 at 14:24
  • I am a bit confused on what your asking but this is the process. –  May 04 '17 at 14:41
  • Take Photo Button - Photo is displayed in imageView - Save Photo Button - Mask is applied to the photo in image view and saved in photo gallery. Hope that makes things more clear. –  May 04 '17 at 14:42
  • I need to know the original orientation of the mask image – Fa.Shapouri May 04 '17 at 14:52
  • I believe the mask is in orientation landscape. However I added the photo of the mask in the top of my question with the name of the link being mask. That image is what is masking the photo in image view. –  May 04 '17 at 14:57
  • because my iPhone os version is higher than my Xcode version I am not able to build this app on my phone and take a photo; but by setting image from assets the result has not problem; So I guess it is going to be fine if you save image before applying mask (I mean save it on camera roll) then check the orientation, and to force user to take photo in portrait maybe http://stackoverflow.com/questions/14484816/force-uiimagepickercontroller-to-take-photo-in-portrait-orientation-dimensions-i help you – Fa.Shapouri May 04 '17 at 15:24
  • What are you sing here " but by setting image from assets the result has not problem" Thanks. –  May 04 '17 at 15:27
  • I have loaded the image of `photo` UIImageView from my local image – Fa.Shapouri May 04 '17 at 15:28
  • Its close the orientation has to go in imagePickercontroller. However I added the red.imageOrientation = UIDevice.current.orientation.imageOrientation. Its creating a error that is stating imageOrientation is a get only property. –  May 04 '17 at 19:48

1 Answers1

0

When you create your UIImage's you need to make sure you give them the correct orientation. This will get the orientation from the phone and apply it to the image

extension UIDeviceOrientation {
    public var imageOrientation: UIImageOrientation? {
        switch self {
        case .portrait: return .right
        case .portraitUpsideDown: return .left
        case .landscapeLeft: return .up
        case .landscapeRight: return .down
        default: return nil
        }
    }
}
image.imageOrientation = UIDevice.current.orientation.imageOrientation
adamfowlerphoto
  • 2,708
  • 1
  • 11
  • 24
  • I think this will work. Where do I call the "image.imageOrientation = UIDevice.current.orientation.imageOrientation". In view did load? –  May 04 '17 at 18:58
  • Wherever you are creating the images. – adamfowlerphoto May 04 '17 at 19:00
  • Where the image is being saved is in func saveImage. That selects the location for the photo to be saved in. Is that where it should go? –  May 04 '17 at 19:05
  • Not sure at exact point you should set it. I'm guessing from the code the orientation is probably lost during your image processing. Check the images returned by UIGraphicsGetImageFromCurrentImageContext(). But it could be original image from the picker, check it's orientation in imagePickerController(_, didFinishPickingMediaWithInfo) – adamfowlerphoto May 04 '17 at 19:16
  • What is "image" in image.imageOrientation = UIDevice.current.orientation.imageOrientation. Is that the name of the image view. –  May 04 '17 at 19:38
  • image is a UIImage – adamfowlerphoto May 04 '17 at 19:39