如何使此imagePickerController能够接受编辑的图像?

时间:2018-07-24 23:10:06

标签: swift uiimagepickercontroller

如何使此imagePickerController能够接受编辑的图像?

选择图像并进行调整(放大和缩小)并选择完成时,图像将恢复为原始状态。我可以在代码中添加些什么,以保存缩放的图像并显示在图像视图中?

以下是我到目前为止的代码:

let cameraRollAction = UIAlertAction(title: "Camera Roll", style: .default) { (action) in
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
        let picker = UIImagePickerController()
        picker.delegate = self
        picker.allowsEditing = true
        picker.mediaTypes = [kUTTypeImage as String]
        picker.sourceType = UIImagePickerControllerSourceType.photoLibrary
        self.present(picker, animated: true, completion: nil)
        self.newPic = false
    }

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let mediaType = info[UIImagePickerControllerMediaType] as! NSString
    if mediaType.isEqual(to: kUTTypeImage as String) {
        let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
        profileImageView.image = selectedImage
        if newPic == true {
            UIImageWriteToSavedPhotosAlbum(selectedImage, self, #selector(imageError), nil)
        }
    }
    self.dismiss(animated: true, completion: nil)
}

2 个答案:

答案 0 :(得分:1)

代替

UIImagePickerControllerOriginalImage

使用

UIImagePickerControllerEditedImage

答案 1 :(得分:0)

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let mediaType = info[UIImagePickerControllerMediaType] as! NSString
if mediaType.isEqual(to: kUTTypeImage as String) {
    let selectedImage = info[UIImagePickerControllerEditedImage] as! UIImage ?? info[UIImagePickerControllerOriginalImage] as! UIImage // will return original image if edited image is not available
    profileImageView.image = selectedImage
    if newPic == true {
        UIImageWriteToSavedPhotosAlbum(selectedImage, self, #selector(imageError), nil)
    }
}
self.dismiss(animated: true, completion: nil)

}