Access to Camera and PhotoLibrary

后端 未结 4 1378
野性不改
野性不改 2021-01-26 03:12

In my iOS app I have an ImageView and two Buttons for opening the camera and the photolibrary. When I click on one of the buttons the app closes. (I\'m running the app on my dev

4条回答
  •  不知归路
    2021-01-26 04:01

    In iOS 10 you need permission to access photoLibrary or camera by adding below keys to your plist and you need to use the proper delegate method.

    To Access Photo Library:

    @IBAction func library(_ sender: UIButton) {
    
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
            imagePicker.allowsEditing = true
            self.present(imagePicker, animated: true, completion: nil)
          } 
        }
    

    To Access Device Camera:

    @IBAction func camera(_ sender: UIButton) {
    
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {    
            imagePicker.delegate = self
            imagePicker.sourceType = UIImagePickerControllerSourceType.camera;
            imagePicker.allowsEditing = false
            self.present(imagePicker, animated: true, completion: nil)
            }
        }
    

    To Pick and display Image:

     func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        ImageDisplay.image = image
        }
        picker.dismiss(animated: true, completion: nil);
     }
    

    Output:

提交回复
热议问题