一尘不染

iOS 11中的照片捕获权限问题

swift

所以这是我的问题。我试图创建一个屏幕,其中有一个UIImageView和一个UIButton。当用户按下按钮时,相机应用程序打开,您拍照,如果您在相机应用程序中按“使用照片”,您将返回到我的应用程序屏幕,并且该照片将放置在我前面提到的UIImageView中。

到目前为止发生的是,当我按下“使用照片”按钮时,图像已正确放置在我的UIImageView中,但是应用程序崩溃并出现以下错误:

该应用已崩溃,因为它尝试访问没有使用说明的隐私敏感数据。
应用程序的Info.plist必须包含一个NSPhotoLibraryAddUsageDescription密钥,该密钥具有字符串值,该字符串值向用户说明该应用程序如何使用此数据。

到目前为止,我所做的是:

  1. 将键“隐私-照片库使用说明”放置为值“ $(PRODUCT_NAME)使用库来处理捕获的照片”。在Info.plist文件中(还检查了它如何以“源代码”形式编写,并且根据Apple开发人员文档是正确的)。

  2. 还应在Info.plist文件中放置键“隐私-相机使用说明”,其值为“ $(PRODUCT_NAME)使用相机”。

  3. 在“目标->->信息->自定义iOS目标属性”下进行检查,并且存在我在步骤1和2中提到的2个键/值对。

到目前为止,我将为您提供代码:

import UIKit
import Vision
import MobileCoreServices
import AVFoundation
import Photos

class ViewController: UIViewController, UIImagePickerControllerDelegate, 
UINavigationControllerDelegate {

var newMedia: Bool?

@IBAction func captureImageButtonPressed(_ sender: Any) {
    //let imageName : String = "dolphin"
    //randomImageView.image = UIImage.init(named:imageName)

    if UIImagePickerController.isSourceTypeAvailable(
        UIImagePickerControllerSourceType.camera) {

        let imagePicker = UIImagePickerController()

        imagePicker.delegate = self
        imagePicker.sourceType =
            UIImagePickerControllerSourceType.camera
        imagePicker.mediaTypes = [kUTTypeImage as String]
        imagePicker.allowsEditing = false

        self.present(imagePicker, animated: true,
                     completion: nil)
        newMedia = true
    }
}

@IBAction func classifyButtonPressed(_ sender: UIButton) {
    performVisionRequest()
}
@IBOutlet weak var randomImageView: UIImageView!
@IBOutlet weak var classificationLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
}

func performVisionRequest() {
    let start = DispatchTime.now()
    let model = Resnet50()
    let request = VNImageRequestHandler(cgImage: randomImageView.image!.cgImage!, options: [:])
    do {
        let m = try VNCoreMLModel(for: model.model)
        let coreMLRequest = VNCoreMLRequest(model: m) { (request, error) in
            guard let observation = request.results?.first as? VNClassificationObservation else { return }
            let stop = DispatchTime.now()
            let nanoTime = stop.uptimeNanoseconds - start.uptimeNanoseconds
            let timeInterval = Double(nanoTime)
            self.classificationLabel.text = "\(observation.identifier) (\(observation.confidence * 100)%) in \(timeInterval) seconds."
        }
        try request.perform([coreMLRequest])
    } catch {
        print(error)
    }
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let mediaType = info[UIImagePickerControllerMediaType] as! NSString
    self.dismiss(animated: true, completion: nil)
    if mediaType.isEqual(to: kUTTypeImage as String) {
        let image = info[UIImagePickerControllerOriginalImage]
            as! UIImage
        randomImageView.image = image
        if (newMedia == true) {
            UIImageWriteToSavedPhotosAlbum(image, self,
                                           #selector(ViewController.image(image:didFinishSavingWithError:contextInfo:)), nil)
        } else if mediaType.isEqual(to: kUTTypeMovie as String) {
            // Code to support video here
        }
    }
}

@objc func image(image: UIImage, didFinishSavingWithError error: NSErrorPointer, contextInfo:UnsafeRawPointer) {
    if error != nil {
        let alert = UIAlertController(title: "Save Failed",
                                      message: "Failed to save image",
                                      preferredStyle: UIAlertControllerStyle.alert)
        let cancelAction = UIAlertAction(title: "OK",
                                         style: .cancel, handler: nil)
        alert.addAction(cancelAction)
        self.present(alert, animated: true,
                     completion: nil)
    }
}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    self.dismiss(animated: true, completion: nil)
}
}

知道为什么我会以粗体显示上述错误吗?非常感谢您抽出宝贵的时间。


阅读 224

收藏
2020-07-07

共1个答案

一尘不染

NSPhotoLibraryAddUsageDescription已在iOS 11中添加。

请像使用其他隐私权限一样,在info.plist中添加“隐私-照片库添加用途说明”和使用说明(字符串)。

参考:https
:
//developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html

2020-07-07