user10002550
user10002550

Reputation:

How can I make this imagePickerController able to accept edited images?

How can I make this imagePickerController able to accept edited images?

When selecting an image and adjusting it (zoom in and out) and selecting done, the image reverts back to its original state. What can I add to to my code to have the zoomed image save and show up in my image view?

The following is my code so far:

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

Upvotes: 0

Views: 96

Answers (2)

Milan
Milan

Reputation: 141

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)

}

Upvotes: 0

Shehata Gamal
Shehata Gamal

Reputation: 100503

Instead of

UIImagePickerControllerOriginalImage

use

UIImagePickerControllerEditedImage

Upvotes: 1

Related Questions