iOS 11 中的照片捕获权限问题 [英] Photo capture permission problems in iOS 11

查看:15
本文介绍了iOS 11 中的照片捕获权限问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

此应用已崩溃,因为它试图在没有使用说明的情况下访问隐私敏感数据.应用程序的 Info.plist 必须包含一个 NSPhotoLibraryAddUsageDescription 键和一个字符串值,向用户解释应用程序如何使用这些数据.

到目前为止我所做的是:

  1. 将键值隐私 - 照片库使用说明"置于值$(PRODUCT_NAME) 使用库以处理您拍摄的照片".在 Info.plist 文件中(还检查了它是如何以 Source 形式编写的,并且根据 Apple Developer Documentation 是正确的).

  2. 还在 Info.plist 文件中放置了值$(PRODUCT_NAME) uses Cameras"的键Privacy - Camera Usage Description".

  3. 在TARGETS->->Info->Custom iOS Target Properties"下检查,我在步骤 1 和 2 中提到的 2 个键/值对存在.

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

导入 UIKit进口愿景导入 MobileCore 服务导入 AVFoundation导入照片类 ViewController: UIViewController, UIImagePickerControllerDelegate,UINavigationControllerDelegate {var newMedia:布尔?@IBAction func captureImageButtonPressed(_ sender: Any) {//让 imageName : String = "dolphin"//randomImageView.image = UIImage.init(named:imageName)如果 UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {让 imagePicker = UIImagePickerController()imagePicker.delegate = selfimagePicker.sourceType =UIImagePickerControllerSourceType.cameraimagePicker.mediaTypes = [kUTTypeImage as String]imagePicker.allowsEditing = falseself.present(imagePicker,动画:真,完成:无)新媒体 = 真}}@IBAction funcclassifyButtonPressed(_ sender: UIButton) {执行视觉请求()}@IBOutlet 弱变量 randomImageView:UIImageView!@IBOutlet 弱变量分类标签:UILabel!覆盖 func viewDidLoad() {super.viewDidLoad()}覆盖 func viewDidLayoutSubviews() {super.viewDidLayoutSubviews()}功能执行视觉请求(){让开始 = DispatchTime.now()让模型 = Resnet50()让请求 = VNImageRequestHandler(cgImage: randomImageView.image!.cgImage!, options: [:])做 {让 m = 尝试 VNCoreMLModel(for: model.model)让 coreMLRequest = VNCoreMLRequest(model: m) { (request, error) in守卫让观察 = request.results?.first as?VNClassificationObservation else { return }让停止 = DispatchTime.now()让 nanoTime = stop.uptimeNanoseconds - start.uptimeNanoseconds让 timeInterval = Double(nanoTime)self.classificationLabel.text = "(observation.identifier) ((observation.confidence * 100)%) 在 (timeInterval) 秒内."}尝试 request.perform([coreMLRequest])} 抓住 {打印(错误)}}func imagePickerController(_picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {让 mediaType = info[UIImagePickerControllerMediaType] 作为!字符串self.dismiss(动画:true,完成:nil)如果 mediaType.isEqual(to: kUTTypeImage as String) {let image = info[UIImagePickerControllerOriginalImage]作为!用户界面图像randomImageView.image = 图像如果(新媒体==真){UIImageWriteToSavedPhotosAlbum(图像,自我,#selector(ViewController.image(image:didFinishSavingWithError:contextInfo:)), nil)} else if mediaType.isEqual(to: kUTTypeMovie as String) {//此处支持视频的代码}}}@objc func image(image: UIImage, didFinishSavingWithError 错误: NSErrorPointer, contextInfo:UnsafeRawPointer) {如果错误 != nil {let alert = UIAlertController(title: "保存失败",消息:无法保存图像",优选样式:UIAlertControllerStyle.alert)let cancelAction = UIAlertAction(title: "OK",样式:.cancel,处理程序:nil)alert.addAction(cancelAction)self.present(警报,动画:真实,完成:无)}}func imagePickerControllerDidCancel(_picker: UIImagePickerController) {self.dismiss(动画:true,完成:nil)}}

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

解决方案

NSPhotoLibraryAddUsageDescription 已在 iOS 11 中添加.

请在 info.plist 中添加隐私 - 照片库添加使用说明"以及使用说明(字符串),就像您为其他隐私权限所做的那样.

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

So here's my problem. I am trying to create a screen in which there is a UIImageView and a UIButton. When the user presses the button, the camera app opens, you take a photo and if you press "Use Photo" in the Camera app, you are returned to my app's screen and the photo is placed in the UIImageView I mentioned previously.

What happens so far is that when I press the "Use Photo" button, the image is correctly placed in my UIImageView but then the app crashes with the following error:

This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSPhotoLibraryAddUsageDescription key with a string value explaining to the user how the app uses this data.

What I've done so far is:

  1. Placed the key "Privacy - Photo Library Usage Description" with the value "$(PRODUCT_NAME) uses Library in order to process the photos you captured." in the Info.plist file (also checked how it is written in Source form and it's correct according to the Apple Developer Documentation).

  2. Also placed the key "Privacy - Camera Usage Description" with the value "$(PRODUCT_NAME) uses Cameras" in the Info.plist file.

  3. Checked under "TARGETS->->Info->Custom iOS Target Properties" and the 2 key/value pairs that I mentioned in steps 1 and 2, exist.

I will provide you with my code so far:

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

Any idea why I get the above error in bold? Thank you very much in advance for your time.

解决方案

NSPhotoLibraryAddUsageDescription was added in iOS 11.

Please add "Privacy - Photo Library Additions Usage Description" in info.plist with a usage description (string), like you did for the other privacy permissions.

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

这篇关于iOS 11 中的照片捕获权限问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆