Add a circular crop component for UIImagePicker?

I have a UIImageView circular border, and I need to add a circular border cropping tool for the UIImagePickerController after the image is selected from the photo library. Very similar to the UIImagePicker's Instagram component. How to add this type of component?

UPDATE

I found this repo with a circular cropping tool https://github.com/ruslanskorb/RSKImageCropper

but can anyone help me implement this crop tool with the UIImagePickerController after the user selects a photo from the photo library?

UPDATE

The following message appears in my debugger:

enter image description here

and the buttons in my crop view are disabled, that is, I can’t select them .. what message is the debugger relay for me?

here is my code:

  @IBAction func chooseProfilePicture(sender: AnyObject) { var myPickerController = UIImagePickerController() myPickerController = UIImagePickerController() myPickerController.delegate = self; myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary self.presentViewController(myPickerController, animated: true, completion: nil) } func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { var image : UIImage = (info[UIImagePickerControllerOriginalImage] as? UIImage)! editProfilePictureImageView.image = image self.dismissViewControllerAnimated(false, completion: { () -> Void in var imageCropVC : RSKImageCropViewController! imageCropVC = RSKImageCropViewController(image: image, cropMode: RSKImageCropMode.Circle) imageCropVC.delegate = self self.navigationController?.pushViewController(imageCropVC, animated: true) }) } 
+5
source share
3 answers

Demo example

Yes, you can add RSKImageCropper to your UIImagePickerController

define imagePicker

 var imagePicker : UIImagePickerController! 

in ViewDidLoad

  imagePicker = UIImagePickerController() imagePicker.delegate = self imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary; self.presentViewController(imagePicker, animated: true, completion: nil) 

Delegate Method:

 func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { var image : UIImage = (info[UIImagePickerControllerOriginalImage] as? UIImage)! picker.dismissViewControllerAnimated(false, completion: { () -> Void in var imageCropVC : RSKImageCropViewController! imageCropVC = RSKImageCropViewController(image: image, cropMode: RSKImageCropMode.Circle) imageCropVC.delegate =self self.navigationController?.pushViewController(imageCropVC, animated: true) }) } 

cm.:

enter image description here

+14
source

Kirit Modi's answer was exactly what I needed, although I needed to do one thing to do this job. For those who do not download the test project, make sure that you have implemented your delegation methods:

Swift 3:

 extension YourViewControllerClass: RSKImageCropViewControllerDelegate { func imageCropViewControllerDidCancelCrop(_ controller: RSKImageCropViewController) { _ = self.navigationController?.popViewController(animated: true) } func imageCropViewController(_ controller: RSKImageCropViewController, didCropImage croppedImage: UIImage, usingCropRect cropRect: CGRect) { self.avatarImageView.image = croppedImage _ = self.navigationController?.popViewController(animated: true) } } 

Swift 2:

 extension YourViewControllerClass: RSKImageCropViewControllerDelegate { func imageCropViewControllerDidCancelCrop(controller: RSKImageCropViewController) { self.navigationController?.popViewControllerAnimated(true) } func imageCropViewController(controller: RSKImageCropViewController, didCropImage croppedImage: UIImage, usingCropRect cropRect: CGRect) { self.avatarImageView.image = croppedImage self.navigationController?.popViewControllerAnimated(true) } } 
+2
source

For Swift 2.2:

add delegate methods to the class:

 class ViewController: UIViewController, UIImagePickerControllerDelegate, RSKImageCropViewControllerDelegate, UINavigationControllerDelegate 

define imagePicker

  var imagePicker : UIImagePickerController! 

in viewDidLoad ()

 imagePicker = UIImagePickerController() imagePicker.delegate = self imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary self.presentViewController(imagePicker, animated: true, completion: nil) 

And the delegate:

 func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!) { let image : UIImage = image picker.dismissViewControllerAnimated(false, completion: { () -> Void in var imageCropVC : RSKImageCropViewController! imageCropVC = RSKImageCropViewController(image: image, cropMode: RSKImageCropMode.Circle) imageCropVC.delegate = self self.navigationController?.pushViewController(imageCropVC, animated: true) }) } 

Of course he needs to make a bridge for the objective class C

Oh, link to RSKImageCrop

0
source

All Articles