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

swift3.0之UIImagePickerController的使用和注意事项,第1张

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

在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)        }            }
总结

以上是内存溢出为你收集整理的swift3.0之UIImagePickerController的使用和注意事项全部内容,希望文章能够帮你解决swift3.0之UIImagePickerController的使用和注意事项所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1061731.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存