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

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

问题描述

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

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.

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

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:

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

到目前为止我所做的是:

What I've done so far is:

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

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

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

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

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

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 已在 iOS 11 中添加.

NSPhotoLibraryAddUsageDescription was added in iOS 11.

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

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

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

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

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