在oc中使用UIImagePickerController只要设置好代理、写代理方法就行了,但是在swift中编译没有什么问题,但是运行的时候会报错,选择一张图片,会显示库是私有的,不能访问,遇到这样的问题很好的解决办法就是在info.plist文件中添加Privacy - Photo Library Usage Description 和Privacy - 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) } }原文链接:https://www.f2er.com/swift/321554.html