Vasil Nunev
Vasil Nunev

Reputation: 450

Swift - UIImage rotated 90 degrees when picked from UIImagePickerController Camera

I am using UIImagePickerController to pick images from both Camera and Library. When i get the image from the library the picture is OK. However, when i get the picture from the camera its rotating 90 degrees to the right. My app works just in portrait mode. Here is the code where i get the image

    @IBAction func camPhoto(sender: AnyObject) {

    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
    imagePicker.allowsEditing = false

    self.presentViewController(imagePicker, animated: true, completion: nil)

}

@IBAction func importPhoto(sender: AnyObject) {

    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    imagePicker.allowsEditing = false

    self.presentViewController(imagePicker, animated: true, completion: nil)

}

my Function importPhoto works fine but the other one doesn't.

    func imagePickerController(picker: UIImagePickerController,
    didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        let selectedPhoto = info[UIImagePickerControllerOriginalImage] as! UIImage

        img.image = selectedPhoto
        dismissViewControllerAnimated(true, completion: {

            self.convBtn.hidden = false

        })
}

Any ideas?

Upvotes: 2

Views: 689

Answers (1)

Ronald Vo
Ronald Vo

Reputation: 64

You can try like :

func imagePickerController(picker: UIImagePickerController,
    didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        let selectedPhoto = info[UIImagePickerControllerOriginalImage] as! UIImage
        **selectedPhoto.withHorizontallyFlippedOrientation()**
        img.image = selectedPhoto
        dismissViewControllerAnimated(true, completion: {

            self.convBtn.hidden = false

        })
}

Upvotes: -1

Related Questions