Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a circular cropping component for UIImagePicker?

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 :

enter image description here

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)

        })

    }
like image 225
John Durand Avatar asked Aug 25 '15 06:08

John Durand


2 Answers

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 :

enter image description here

like image 95
Kirit Modi Avatar answered Nov 05 '22 06:11

Kirit Modi


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

}
like image 38
Josh O'Connor Avatar answered Nov 05 '22 06:11

Josh O'Connor