照片选择器封装(swift3.0+)

本文介绍了一个使用Swift封装的照片选择器组件,支持从相册选择图片及通过相机拍摄,并提供了图片裁剪功能。该组件采用UIImagePickerController进行图片选择,并通过自定义控制器实现功能整合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这是一个swift封装的照片选择器,包含了照片选择和拍照,支持裁剪

class PhotoPicker: NSObject,UIImagePickerControllerDelegate,UINavigationControllerDelegate{
    
    weak var vc : UIViewController?
    var callBack : ((_ image: UIImage) -> ())?
    var pickerVc : UIImagePickerController!
    /// 是否允许裁剪
    var allowsEditing = true
    /// 裁剪尺寸
    var cropperImageSize : CGSize!
    
    
    /**
     选取照片,并且裁剪
     // 默认裁剪ScreenWidth*2  ScreenWidth*2
     */
    func show(_ viewConttoller : UIViewController, cropperImageSize : CGSize = CGSize(width: ScreenWidth*2, height: ScreenWidth*2),allowsEditing : Bool = true,back :@escaping (_ image: UIImage) -> ())  {
        let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        
        callBack = back
        vc = viewConttoller
        let pick:UIImagePickerController = UIImagePickerController()
        pickerVc = pick
        pick.allowsEditing = true
        pick.delegate = self
        self.allowsEditing = allowsEditing
        self.cropperImageSize = cropperImageSize
        if UIImagePickerController.isSourceTypeAvailable(.camera) {
            let paizhaoAction = UIAlertAction(title: "拍照", style: .default, handler:{ [unowned viewConttoller] alertAction   in
                pick.sourceType = .camera
                viewConttoller.present(pick, animated: true,
                    completion: {
                })
                })
            alertController.addAction(paizhaoAction)
        }
        
        let xiangcetAction = UIAlertAction(title: "相册", style: .default, handler:{  [unowned viewConttoller] alertAction in
            pick.sourceType = .photoLibrary
            viewConttoller.present(pick, animated: true, completion: nil)
            })
        let canelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        
        alertController.addAction(xiangcetAction)
        alertController.addAction(canelAction)
        alertController.view.tintColor = COLOR_MAIN
        viewConttoller.present(alertController, animated: true, completion: nil)
    }
    
    //MARK: - 相册选中代理
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        var image : UIImage
        if self.allowsEditing { // 是不是需要裁剪
            let editImage = info[UIImagePickerControllerEditedImage] as! UIImage
            if editImage.size.height > self.cropperImageSize.height {
                image = UIImage.resizeImage(editImage, newSize: self.cropperImageSize)
            }else {
                self.cropperImageSize = CGSize(width: ScreenWidth*2, height: editImage.size.height)
                image = UIImage.resizeImage(editImage, newSize: self.cropperImageSize)
            }
        }else {
            image =  info[UIImagePickerControllerOriginalImage] as! UIImage
        }
        if callBack != nil {  // 回调
            callBack!(image)
        }
        picker.dismiss(animated: false, completion: nil)
    }
    
    
    //MARK: - 取消选中代理
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController){
        picker.dismiss(animated: true) {
        }
    }
    
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值