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

标签 ios xcode8 uiimagepickercontroller swift4

我正在使用 UIImagePickerController,因为我正在创建一个允许通过照片库和相机发布图像的应用程序。我创建了一个基本演示,其中包含一个 UIImageView、UIButton 以及一些用于设置 UIImagePickerController 的代码。此外,我还在Xcode的plist部分设置了(NSPhotoLibraryUsageDescription和NSCameraUsageDescription)。图像拾取功能工作得很好,但当我运行模拟器时,我没有被询问是否应该让应用程序允许访问我的相机或照片库。然后我尝试关闭 plist 语句并再次运行。没有这些,应用程序应该会崩溃,但不会。我的问题是我在这里做错了什么让选择器在没有 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

最佳答案

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

这是由于 iOS 11 中的更改。您不再需要用户授权仅通过 UIImagePickerController 接收 UIImage。

但如果您想要更深入的信息,即访问作为 PHAsset 的图像及其元数据,您确实需要用户授权。

当然,如果您希望此应用在 iOS 10 及更早版本上运行,您仍然需要用户授权。

关于ios - UIImagePickerController 在没有 NS 照片库和相机描述的情况下工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48413392/

相关文章:

iOS 在应用程序启动时动画 subview ?

ios - Xcode 8 说供应配置文件具有 aps-environment 权利但导出后存档 aps-environment 丢失

ios - 从不兼容类型 'id<UINavigationControllerDelegate,UIImagePickerControllerDelegate>' 分配给 'ViewController *const__strong'

iphone - cameraOverlayView 在 UIImagePickerController 中裁剪结果

ios - 仅在 Testflight 构建时应用程序崩溃

php - 如何使用 Objective-C 在 iOS 上执行相同的加密/解密 PHP 函数?

iphone - 将已发布的 iPhone/iPad 应用程序转换为通用

swift - Xcode 8 代码覆盖率

swift3 - Swift中previewLayer的AVCapture和缩放

ios - 如何在 Swift 4 中通过具有多个参数的多部分表单数据上传图像或文件