swift 为UIImagePicker添加圆形裁剪组件?

vsaztqbk  于 2023-08-02  发布在  Swift
关注(0)|答案(3)|浏览(107)

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

更新

我发现这个仓库有一个循环裁剪工具https://github.com/ruslanskorb/RSKImageCropper
但是有人能指导我如何在用户从照片库中选择照片后使用UIImagePickerController实现这个裁剪工具吗?

更新

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


的数据
我的裁剪视图中的按钮被禁用,这意味着我无法选择它们。调试器传递给我的是什么消息?
下面是我的代码:

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

        })

    }

字符串

gk7wooem

gk7wooem1#

Example Demo
是的,您可以在UIImagePickerController中添加RSKImageCropper
定义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, 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)

    })

}


参见:


的数据

tp5buhyn

tp5buhyn2#

Kirit Modi的回答正是我所需要的,尽管我需要做一件事来实现这一目标。对于那些没有下载测试项目的人,请确保实现了委托方法:
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)
    }

}

jecbmhm3

jecbmhm33#

对于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

相关问题