简体   繁体   English

UIImagePickerController在没有NS照片库和相机描述的情况下工作

[英]UIImagePickerController working without NS Photo Library and Camera descriptions

I am working with UIImagePickerController because I am creating an application that allows for the posting of images through both the photo library and camera. 我正在使用UIImagePickerController,因为我正在创建一个应用程序,该应用程序允许通过照片库和相机发布图像。 I created a basic demo that consists of a UIImageView, UIButton with some code to set up the UIImagePickerController. 我创建了一个基本的演示,其中包含一个UIImageView,UIButton和一些用于设置UIImagePickerController的代码。 In addition, I have also set up (NSPhotoLibraryUsageDescription and NSCameraUsageDescription) in the plist section of Xcode. 此外,我还在Xcode的plist部分中设置了(NSPhotoLibraryUsageDescription和NSCameraUsageDescription)。 The image picking function works beautifully yet when I run the simulator I am not being queried on whether or not I should let the app allow access to either my camera or photo library. 图像拾取功能可以很好地运行,但是当我运行模拟器时,没有询问我是否应该让该应用程序允许访问我的相机或照片库。 I then tried taking the plist statements off and running again. 然后,我尝试取消plist语句并再次运行。 Without these, the app should crash however it does not. 没有这些,应用程序应该崩溃,但不会崩溃。 My question is what am I doing wrong here for the picker to work without the plist and why does the app not ask for permissions with the NS usage statements? 我的问题是,在没有plist的情况下,选择器无法正常工作,我在做什么?为什么应用程序不使用NS用法语句请求权限?

import UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {


@IBOutlet weak var imageView: UIImageView!


@IBAction func importImage(_ sender: UIButton) {

    let imagePickerController = UIImagePickerController()
    imagePickerController.delegate = self

    let actionSheet = UIAlertController(title: "Add Your Profile Picture", message: "Choose An Option", preferredStyle: . actionSheet)

    actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (action:UIAlertAction) in
        if UIImagePickerController.isSourceTypeAvailable(.camera) {
            imagePickerController.sourceType = .camera
            self.present(imagePickerController, animated: true, completion: nil)
        }
        else {
            print("Camera not Available")
        }
    }))

    actionSheet.addAction(UIAlertAction(title: "Camera Roll", style: .default, handler:{ (action:UIAlertAction) in imagePickerController.sourceType = .photoLibrary
        self.present(imagePickerController, animated: true, completion: nil)
    }))

    actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil
    ))

    self.present(actionSheet, animated: true, completion: nil)

}


func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
    let image = info[UIImagePickerControllerOriginalImage] as? UIImage

    imageView.image = image

    picker.dismiss(animated: true, completion: nil)
}


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




override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

} }

Storyboard View 故事板视图

plist image plist图片

why does the app not ask for permissions with the NS usage statements 为什么应用程序不使用NS使用说明请求权限

This is due to a change in iOS 11. You no longer need user authorization merely to receive a UIImage through the UIImagePickerController. 这是由于iOS 11中的更改​​而引起的。您不再仅需要通过UIImagePickerController接收UIImage的用户授权。

But if you want deeper information, ie access to the image as a PHAsset and its metadata, you do need user authorization. 但是,如果您想获得更深的信息,即以PHAsset的形式访问图像及其元数据,则确实需要用户授权。

And of course if you want this app to run on iOS 10 and before, you'll still need user authorization. 当然,如果您希望此应用程序在iOS 10及更高版本上运行,则仍然需要用户授权。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM