I Have a circular framed UIImageView
and I need to add a circular framed cropping tool for the UIImagePickerController
, after the image is selected from a photo library. Very similar to Instagram's UIImagePicker's
crop component. How do I add this type of component?
UPDATE
I've found this repo with a circular cropping tool https://github.com/ruslanskorb/RSKImageCropper
but can someone guide me on to how to implement this cropping tool with the UIImagePickerController after the user selects a photo from the photo library?
UPDATE
I am getting the following message in my debugger :
and the buttons in my crop view are disabled, meaning I cannot select them.. what message is the debugger relaying on to 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)
})
}
Example Demo
Yes you can add RSKImageCropper
in 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 methode :
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)
})
}
see :
Kirit Modi's answer was exactly what I needed, although I needed to do one thing to make this work. For those that dont download the test project, make sure to implement your delegate 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)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With