Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

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

}

Storyboard View

plist image

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...