尝试使用照片库/相机中的图像更新UIImageView。 似乎与访问相机/照片库的权限有关的一些错误
完全错误:发现]发现扩展时遇到错误:错误Domain = PlugInKit代码= 13“查询已取消” UserInfo = {NSLocalizedDescription =查询已取消}
import Photos
import UIKit
import MapKit
class detailViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var selectedName: String?
var selectedImage: UIImage?
var selectedText: String?
@IBOutlet weak var cityName: UILabel!
@IBOutlet weak var cityImage: UIImageView!
@IBOutlet weak var cText: UILabel!
@IBOutlet weak var map: MKMapView!
@IBOutlet weak var lat: UILabel!
@IBOutlet weak var lon: UILabel!
@IBOutlet weak var searchText: UITextField!
@IBOutlet weak var img_view: UIImageView!
let imagePickerController = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
self.cityName.text = selectedName!
self.cityImage.image = selectedImage!
self.cText.text = selectedText!
imagePickerController.delegate = self
}
var picker = UIImagePickerController()
@IBAction func pickButton(_ sender: UIButton) {
checkPermission()
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Take Photo", style: .default, handler: { _ in
self.camera()
}))
alert.addAction(UIAlertAction(title: "Select Photo", style: .default, handler: { _ in
self.gallery()
}))
alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
func camera(){
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)){
picker.sourceType = UIImagePickerControllerSourceType.camera
picker.allowsEditing = true
picker.delegate = self
self.present(picker, animated: true, completion: nil)
}
else{
let alert = UIAlertController(title: "Warning", message: "You don't have camera", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
func gallery(){
picker.sourceType = UIImagePickerControllerSourceType.photoLibrary
picker.allowsEditing = true
picker.delegate = self
self.present(picker, animated: true, completion: nil)
}
func checkPermission() {
let photoAuthorizationStatus = PHPhotoLibrary.authorizationStatus()
switch photoAuthorizationStatus {
case .authorized:
print("Access is granted by user")
case .notDetermined:
PHPhotoLibrary.requestAuthorization({
(newStatus) in
print("status is \(newStatus)")
if newStatus == PHAuthorizationStatus.authorized {
print("success")
}
})
print("It is not determined until now")
case .restricted:
print("User do not have access to photo album.")
case .denied:
print("User has denied the permission.")
}
}
}
答案 0 :(得分:2)
我不确定是什么导致了该错误,但如果它试图访问任何照片/相机,则可能会出现问题,因为您从未真正检查过用户是否有权访问相机或照片库。
您已经定义了checkPermission方法(通常您嵌套在viewDidLoad()
内),但从未真正使用过。
在尝试访问照片资源之前,您需要重组代码以实际检查权限。
您还需要注意PHPhotoLibrary.requestAuthorization()
立即返回。如果您已经具有权限,那么很好,但是如果需要授予权限,则需要管理此权限的异步性质。使用完成处理程序来控制后续操作,但是请注意,该块可以在后台线程上运行,因此您需要将所有UI工作都强制到主线程上。