How to get the edited image from UIImagePickerController in Swift?
Solution 1
SWIFT 3.0:
Add UINavigationControllerDelegate
and UIImagePickerControllerDelegate
var imagePicker = UIImagePickerController()
@IBAction func btnSelectPhotoOnClick(_ sender: UIButton)
{
let alert = UIAlertController(title: "Choose Image", message: nil, preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Camera", style: .default, handler: { _ in
self.openCamera()
}))
alert.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { _ in
self.openGallary()
}))
alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))
imagePicker.delegate = self
self.present(alert, animated: true, completion: nil)
}
func openCamera()
{
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.camera))
{
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}
else
{
let alert = UIAlertController(title: "Warning", message: "You don't have camera", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
func openGallary()
{
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
picker.dismiss(animated: true, completion: nil)
//You will get cropped image here..
if let image = info[UIImagePickerControllerEditedImage] as? UIImage
{
self.imageView.image = image
}
}
Solution 2
You can do it like this.
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let image = info[UIImagePickerControllerEditedImage] as UIImage
}
Solution 3
I found the cause of the issue! Its because of the property "allowsEditing" which was set to false! I have changed that to true and now it is working fine.
Solution 4
Swift 4.2
if let editedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
// Use editedImage Here
}
Comments
-
Developer almost 2 years
I have seen and read the documents of Apple where they have clearly mentioned about the key UIImagePickerControllerEditedImage. But when I am running my following code, I am not getting that key in the Dictionary. What is the reason behind this?
My Code:
func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info:NSDictionary!) { println(info) //var tempImage:UIImage = info[UIImagePickerControllerOriginalImage] as UIImage var tempImage:UIImage? = info[UIImagePickerControllerEditedImage] as? UIImage profileButton.setImage(tempImage, forState: UIControlState(0)) self.dismissViewControllerAnimated(true, completion:nil) }
println(info) prints:
{ UIImagePickerControllerMediaType = "public.image"; UIImagePickerControllerOriginalImage = "<UIImage: 0x178295d60> size {960, 960} orientation 0 scale 1.000000"; UIImagePickerControllerReferenceURL = "assets-library://asset/asset.JPG?id=2DDCE06B-6082-4167-A631-B3DF627055DF&ext=JPG"; }
FYI, I am picking image from Library and I tried from Camera also, but this key is still not coming but the other data about image is coming. And I am using iOS8.0 build SDK.
So how can I use
UIImagePickerControllerEditedImage
if there is no such key coming in the dictionary. -
Developer over 9 yearsYou are fetching value for key "UIImagePickerControllerEditedImage" but its not present in the dictionary "info". This is the problem I am facing!
-
sbarow over 9 yearsI have tested this on an iPad with iOS 7 and simulator with iOS 8 and it works as well as shows the
UIImagePickerControllerEditedImage
when youprintln("\(info)")
I did notice a difference in the way things are printed when changingdidFinishPickingMediaWithInfo info: [NSObject : AnyObject]
todidFinishPickingMediaWithInfo info:NSDictionary!
do you get some kind of error if you use the above code? What version of Xcode are you using? -
Developer over 9 yearsDude! I have mentioned the response also in my question and I also mentioned that base SDK is 8.0 so obviously I am using xcode 6X. BTW I am using XCode 6.1 (not Beta)
-
Fattie about 8 yearshi @Developer - you really need to TICK this answer.
-
Swapnil Dhotre over 7 yearsHi @Anas could you please tell how to pick edited video using swift. I am able to use normal video but not able to get edited one.
-
EnasAZ almost 5 yearsI got this error Cannot subscript a value of type '[String : AnyObject]' with an index of type 'UIImagePickerController.InfoKey'