为 UIImagePicker 添加圆形裁剪组件?
Posted
技术标签:
【中文标题】为 UIImagePicker 添加圆形裁剪组件?【英文标题】:Add a circular cropping component for UIImagePicker? 【发布时间】:2015-08-25 06:24:04 【问题描述】:我有一个圆形框UIImageView
,在从照片库中选择图像后,我需要为UIImagePickerController
添加一个圆形框裁剪工具。与 Instagram 的 UIImagePicker's
裁剪组件非常相似。如何添加这种类型的组件?
更新
我用圆形裁剪工具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)
)
【问题讨论】:
这可以在 UIImagePicker 中实现吗? @KiritModi 我在我的答案下载中添加了示例演示并运行它。 【参考方案1】: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)
)
见:
【讨论】:
你有没有机会把它改成 swift? 你知道如何在swift中使用objective C class 我知道如何通过桥接头导入,但我需要转换为 swift 的只是这部分答案:[picker dismissViewControllerAnimated:false completion:^ RSKImageCropViewController *imageCropVC = [[RSKImageCropViewController alloc] initWithImage:image cropMode:RSKImageCropModeCircle]; imageCropVC.delegate = self; [self.navigationController pushViewController:imageCropVC animated:YES]; ];
选择按钮被禁用,我无法选择它,我做错了什么?
是的,操作无法完成是因为您的 JSON 响应来自 Web 服务无效。【参考方案2】:
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)
【讨论】:
我无法在我的项目中添加这个,你能告诉我如何实现这个。我想添加用于编辑图像的圆形组件。 我可以帮助你。您是否添加了 RSKImageCropView 库?您可以使用 cocoaPod 或将整个库拖到您的项目中。 你能给我提供 RSKImageCropView 的确切链接吗,我正在使用 swift 2.2。 谷歌一下。这是第一个结果。 github.com/ruslanskorb/RSKImageCropper 我还建议升级到 swift 3.0。您将不得不在某个时候。【参考方案3】:对于 Swift 2.2:
向您的类添加委托方法:
class ViewController: UIViewController, UIImagePickerControllerDelegate, RSKImageCropViewControllerDelegate, UINavigationControllerDelegate
定义图像选择器
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
【讨论】:
以上是关于为 UIImagePicker 添加圆形裁剪组件?的主要内容,如果未能解决你的问题,请参考以下文章