加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift3.0之UIImagePickerController的使用和注意事项

发布时间:2020-12-14 06:14:08 所属栏目:百科 来源:网络整理
导读:在oc中使用UIImagePickerController只要设置好代理、写代理方法就行了,但是在swift中编译没有什么问题,但是运行的时候会报错,选择一张图片,会显示库是私有的,不能访问,遇到这样的问题很好的解决办法就是在info.plist文件中添加 Privacy - Photo Librar

在oc中使用UIImagePickerController只要设置好代理、写代理方法就行了,但是在swift中编译没有什么问题,但是运行的时候会报错,选择一张图片,会显示库是私有的,不能访问,遇到这样的问题很好的解决办法就是在info.plist文件中添加Privacy - Photo Library Usage DescriptionPrivacy - Camera Usage Description 两个字符串属性

下面是代码片段

1.在类中设置代理方法

UIImagePickerControllerDelegate,UINavigationControllerDelegate


2.UIImagePickerController对象的创建

 lazy var imagePicker: UIImagePickerController = {
        let imgPicker = UIImagePickerController()
       imgPicker.modalPresentationStyle = UIModalPresentationStyle.fullScreen
        imgPicker.allowsEditing = true
        imgPicker.delegate = self 
        return imgPicker
    }()

3.图片获取的选择模式,以及UIImagePickerController的代理方法实现

func selectHeadImageFromCamera() {
        
        let alertVC = UIAlertController(title: nil,message: nil,preferredStyle: .actionSheet)
        
        let cancelAction = UIAlertAction(title: "取消",style: .default) { (action) in
            
            
        }
        
       
        weak var weakSelf = self
        let cameralAction = UIAlertAction(title: "拍照",style: .default) { (action) in
            
            weakSelf?.clickedButtonAtIndex(index: 0)
        }
        
        let pictureAction = UIAlertAction(title: "相册",style: .default) { (action) in
            
             weakSelf?.clickedButtonAtIndex(index: 1)
        }
        
        alertVC.addAction(cameralAction)
        alertVC.addAction(pictureAction)
        alertVC.addAction(cancelAction)
        
        self.present(alertVC,animated: true,completion: nil)
        
    }
    
    //#pram mark --UIImagePickerControllerDelegate
    
    func imagePickerController(_ picker: UIImagePickerController,didFinishPickingMediaWithInfo info: [String : Any]) {
        
        self.hideHud()
        self.showHud(in: self.view,hint: "上传中...")
        let image:UIImage = info[UIImagePickerControllerOriginalImage] as! UIImage
        picker.dismiss(animated: true,completion: nil)
        
        if image != nil {
            
             self.updateUserHeadPortrait(image: image)
        }
        else
        {
            self.hideHud()
            self.showHint("上传失败")
        }
        
    }
    
    func updateUserHeadPortrait(image:UIImage){
        
      
        let newImage = self.imageWithImage(image: image,scaleToSize: CGSize(width: 150.0,height: 150.0))
        weak var weakSelf = self
        if newImage != nil {
            weak var weakSelf = self
            NetService.apiUpdateUserHeadPortait(newImage,success: { (success) in
                
                weakSelf?.imageView?.image = newImage
                ManagerTool.shared.currentUserInfo.babyHeadPortrait = success
                weakSelf?.hideHud()
                weakSelf?.showHint("上传成功")
                
            },failure: { (failure) in
                weakSelf?.hideHud()
                weakSelf?.showHint("上传失败")
            })
        }
    }
    
    func imageWithImage(image:UIImage,scaleToSize:CGSize) -> UIImage {
        
        UIGraphicsBeginImageContext(scaleToSize)
        image.draw(in: CGRect(x: 0,y: 0,width: scaleToSize.width,height: scaleToSize.height))
        let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        
        return newImage
    }
    
    
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        
        self.imagePicker.dismiss(animated: true,completion: nil)
    }
    
    func clickedButtonAtIndex(index:Int) {
        
        
        if index == 0
        {
            print("(index)")
           
            if Platform.isSimulator
            {
                self.hideHud()
                self.showHint("模拟器不支持拍照功能")
            }
            else
            {
                if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){
                    self.imagePicker.sourceType = UIImagePickerControllerSourceType.camera
                    
                    if UIImagePickerController.isCameraDeviceAvailable(UIImagePickerControllerCameraDevice.front){
                        self.imagePicker.cameraDevice = UIImagePickerControllerCameraDevice.front
                    }
                    self.imagePicker.mediaTypes = [kUTTypeImage as String]
                    self.present(self.imagePicker,completion: nil)
                }
            }

        }
        else if index == 1{
            
            self.imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
            self.imagePicker.mediaTypes = [kUTTypeImage as String];
            self.present(self.imagePicker,completion: nil)
        }
        
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读