1

crash when access UIImagePickerController with photoLibrary sourceType ; privacy was added to info.plist

let myPickerController = UIImagePickerController()
myPickerController.delegate = self
myPickerController.sourceType = .photoLibrary
self.present(myPickerController, animated: true, completion: nil)'

error :

unrecognized selector sent to instance 0x28184fb40 2019-09-09 12:45:52.126598+0300 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType encodeWithCoder:]: unrecognized selector sent to instance 0x28184fb40'

Kathiresan Murugan
  • 2,783
  • 3
  • 23
  • 44
Hla Khader
  • 11
  • 3
  • What exactly did you put in Info.plist? – David Steppenbeck Sep 09 '19 at 11:45
  • I don't think there's a problem with this code actually. Do you tap a button to execute that code? It's possibly a problem with the button. If you comment out your code related to the picker controller and just tap the button, does it still crash? – David Steppenbeck Sep 09 '19 at 13:35

2 Answers2

1

I'm assuming that you tap a button to start your UIImagePickerController. I suspect that you might have your button connected to more than one IBAction method, if you designed it with interface builder.

Go to your storyboard and right click on the button. If the button is connected to multiple 'Sent Events' then that's your problem. Delete the one(s) that are irrelevant (perhaps an old one that you deleted in code is still connected).

0

Try this:

    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self

    let cameraAction = UIAlertController(title: "Title", message: "Choose to upload a picture", preferredStyle: .alert)
    cameraAction.addAction(UIAlertAction(title: "Camera", style: .default, handler: {(action) in

        imagePicker.sourceType = UIImagePickerController.SourceType.camera
        self.present(imagePicker, animated: true){

        }
    }))
    cameraAction.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: {(action) in

        imagePicker.sourceType = .photoLibrary
        self.present(imagePicker, animated: true){

        }
    }))
    imagePicker.allowsEditing = true
    self.present(cameraAction, animated: true)
Suhail
  • 324
  • 1
  • 15
  • How is that code for any different when selecting source type .photoLibrary? – David Steppenbeck Sep 09 '19 at 12:40
  • Here, in my code I'm presenting the UIAlertController with two actions of Camera and photo library. While @Hla Khader is trying to present the UIImagePickerController directly. – Suhail Sep 09 '19 at 12:43
  • Yes, but my point is that if you select "Photo Library" then the same code is executed. The poster did not ask how to get images from the camera. – David Steppenbeck Sep 09 '19 at 12:45
  • @DavidSteppenbeckPhD is there any problem that I'm providing solution with two more options? Also, if the user needs to implement with the Camera option in future then it'll be helpful. – Suhail Sep 09 '19 at 12:49
  • Your answer does not provide a solution to the question that was asked, that is what I'm trying to say. – David Steppenbeck Sep 09 '19 at 12:56
  • Let the questioner decide whether I'm providing a proper solution or not. – Suhail Sep 09 '19 at 12:59