Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force UIImagePickerController to Crop a Square Image

How do we force UIIImagePickerController to crop a square image?

I have searched all over and I haven't found a solid solution. Thankyo

var imagePickerController: UIImagePickerController = UIImagePickerController();
imagePickerController.allowsEditing = true;
imagePickerController.delegate = self;
imagePickerController.sourceType = sourceType;



func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
    profilePictureSelected = true;

    profilePictureImageView.image = image;

    picker.dismissViewControllerAnimated(true, completion: nil);
}
like image 932
JayVDiyk Avatar asked Aug 26 '15 07:08

JayVDiyk


2 Answers

You are doing it correctly up until you receive your delegate callback, within the callback you need to specify that it's the edited image that you want to use. Please note that i'm using a different delegate method here.

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
    if let chosenImage = info[UIImagePickerControllerEditedImage] as? UIImage {

        profilePictureSelected = true;

        profilePictureImageView.image = chosenImage;
    }
    picker.dismissViewControllerAnimated(true, completion: nil);
}
like image 98
Swinny89 Avatar answered Oct 29 '22 10:10

Swinny89


For Swift 4

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
       viewController.dismiss(animated: true, completion: { () -> Void in
           if let image = info[UIImagePickerControllerEditedImage] as! UIImage {
               yourUIImageView.image = image
           }
       })
}
like image 1
Supanat Techasothon Avatar answered Oct 29 '22 10:10

Supanat Techasothon