Force UIImagePickerController to Crop a Square Image

馋奶兔 提交于 2019-12-09 14:40:33

问题


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

回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/32220958/force-uiimagepickercontroller-to-crop-a-square-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!