【问题标题】:ImagePicker error: Error Domain=PlugInKit Code=13ImagePicker 错误:错误域=PlugInKit 代码=13
【发布时间】:2020-03-19 19:52:38
【问题描述】:

尝试使用照片库/相机中的图像更新 UIImageView。 似乎与访问相机/照片库的权限有关的一些错误

完全错误:发现] 发现扩展时遇到的错误:Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled}

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.")
        }
    }



}

【问题讨论】:

    标签: swift xcode


    【解决方案1】:

    我不确定产生错误的 PlugInKit 是什么,但如果它试图访问任何照片/相机,它很可能会出现问题,因为您从未真正检查过用户是否有权访问相机或照片库.

    您已经定义了 checkPermission 方法(通常您已将其嵌套在您的 viewDidLoad() 中),但您从未真正使用过它。

    在尝试访问照片资源之前,您需要重新构建代码以实际检查权限。

    您还需要注意PHPhotoLibrary.requestAuthorization() 会立即返回。如果您已经拥有权限,那很好,但如果您需要被授予权限,您将需要管理它的异步性质。使用完成处理程序来控制后续操作,但请注意,该块可以在后台线程上运行,因此您需要将任何 UI 工作强制到主线程上。

    【讨论】:

    • 将 checkPermission 函数移到 viewDidLoad() 之外,并在 func pickButton 中调用它。我现在弹出询问是否允许设备访问照片库,这很好,但仍然收到“查询已取消”错误
    • 好的开始:)。但是现在有一个问题,checkPermissions() 之后的代码将在弹出窗口被回答之前运行并且您知道是否已授予权限。您最好检查是否授予了权限,如果是,则调用一个显示对话框的单独函数,如果不调用 requestAuthorization,然后在授予权限时在完成处理程序中调用该单独的函数。
    • 以上都需要修复,但我认为这不会最终解决您的问题。如果您在询问之前已经搜索过 SO,您会发现这个 stackoverflow.com/questions/44465904/… 包含很多关于此错误的信息。
    猜你喜欢
    • 2017-11-11
    • 1970-01-01
    • 2018-03-18
    • 2013-05-25
    • 2019-02-11
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    相关资源
    最近更新 更多