为UIImagePicker添加循环裁剪组件?

时间:2015-08-25 06:24:04

标签: ios swift uiimageview uiimagepickercontroller

我有一个圆形框架UIImageView,我需要在从照片库中选择图像后为UIImagePickerController添加一个圆形框架裁剪工具。与Instagram的UIImagePicker's裁剪组件非常相似。如何添加此类组件?

更新

我用圆形裁剪工具https://github.com/ruslanskorb/RSKImageCropper

找到了这个回购

但有人可以指导我在用户从照片库中选择照片后如何使用UIImagePickerController实现此裁剪工具吗?

更新

我在调试器中收到以下消息:

enter image description here

并且我的裁剪视图中的按钮被禁用,这意味着我无法选择它们。调试器将什么消息转发给我?

这是我的代码:

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

        })

    }

2 个答案:

答案 0 :(得分:3)

Kirit Modi的回答正是我所需要的,尽管我需要做一件事来完成这项工作。对于那些不下载测试项目的人,请确保实现您的委托方法:

斯威夫特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)
    }

}
斯威夫特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)
    }

}

答案 1 :(得分:0)

对于Swift 2.2:

向您的班级添加委托方法:

class ViewController: UIViewController, UIImagePickerControllerDelegate, RSKImageCropViewControllerDelegate, UINavigationControllerDelegate 

定义imagePicker

    var imagePicker : UIImagePickerController!

在viewDidLoad()

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

委托方法:

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

当然需要为目标C类做桥梁

哦,链接到RSKImageCrop