UIImagePickerController在没有NS照片库和相机描述的情况下工作 [英] UIImagePickerController working without NS Photo Library and Camera descriptions

查看:81
本文介绍了UIImagePickerController在没有NS照片库和相机描述的情况下工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用UIImagePickerController,因为我正在创建一个应用程序,该应用程序允许通过照片库和照相机发布图像.我创建了一个基本演示,该演示包含一个UIImageView,UIButton和一些用于设置UIImagePickerController的代码.此外,我还在Xcode的plist部分中设置了(NSPhotoLibraryUsageDescription和NSCameraUsageDescription).图像拾取功能可以很好地运行,但是当我运行模拟器时,并没有询问我是否应该让该应用程序允许访问我的相机或照片库.然后,我尝试取消plist语句,然后再次运行.没有这些,应用程序应该崩溃,但不会崩溃.我的问题是,在没有plist的情况下,选择器无法正常工作,我在做什么?为什么应用程序不要求使用NS用法声明的权限?

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. I created a basic demo that consists of a UIImageView, UIButton with some code to set up the UIImagePickerController. In addition, I have also set up (NSPhotoLibraryUsageDescription and NSCameraUsageDescription) in the plist section of Xcode. 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. 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?

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

}

故事板视图

plist图片

推荐答案

应用为何不通过NS使用说明请求权限

why does the app not ask for permissions with the NS usage statements

这是由于iOS 11的更改而引起的.您不再仅通过UIImagePickerController接收UIImage便需要用户授权.

This is due to a change in iOS 11. You no longer need user authorization merely to receive a UIImage through the UIImagePickerController.

但是,如果您想获取更深的信息,即作为PHAsset及其元数据访问图像,则要做需要用户授权.

But if you want deeper information, i.e. access to the image as a PHAsset and its metadata, you do need user authorization.

当然,如果您希望该应用程序在iOS 10及更高版本上运行,则仍然需要用户授权.

And of course if you want this app to run on iOS 10 and before, you'll still need user authorization.

这篇关于UIImagePickerController在没有NS照片库和相机描述的情况下工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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