Swift - 从UIImagePickerController Camera中挑选时,UIImage旋转了90度

时间:2016-03-24 02:12:37

标签: ios swift uiimage uiimagepickercontroller

我正在使用UIImagePickerController从Camera和Library中选择图像。当我从库中获取图像时,图片就可以了。然而,当我从相机获取图片时,它向右旋转90度。我的应用程序仅在纵向模式下工作。这是我得到图像的代码

    @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)

}

我的函数importPhoto工作正常,但另一个没有。

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

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

            self.convBtn.hidden = false

        })
}

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试:

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

        })
}