Search code examples
swiftuiimagepickercontroller

Can someone please tell me why my apps crashes when I press the cam button


I had a class that i chose to remove and after that my add keeps crashing.

Below is what is in the button

@IBAction func cameraButton_TouchUpInside(_ sender: Any) {
        let imagePickerController = ImagePickerController()
        imagePickerController.delegate = self
        imagePickerController.imageLimit = 1
        present(imagePickerController, animated: true, completion: nil)
    }

func wrapperDidPress(_ imagePicker: ImagePickerController, images: [UIImage]) {

    }
    func doneButtonDidPress(_ imagePicker: ImagePickerController, images: [UIImage]) {
        guard let image = images.first else {
            dismiss(animated: true, completion: nil)
            return
        }


   if let image = info["UIImagePickerControllerOriginalImage"] as? UIImage{
            selectedImage = image
            photo.image = image
            dismiss(animated: true, completion: nil)
//            dismiss(animated: true, completion: {
//                self.performSegue(withIdentifier: "Camera", sender: nil)
//            })
        }

if I uncomment the what is above my app crashes when i try to choose from the library

more

//    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//        if segue.identifier == "filter_segue" {
//            let filterVC = segue.destination as! FilterViewController
//            filterVC.selectedImage = self.selectedImage
//            filterVC.delegate = self
//        }
   // }

//extension CameraViewController: FilterViewControllerDelegate {
//    func updatePhoto(image: UIImage) {
//        self.photo.image = image
//        self.selectedImage = image
//    }
//}

Solution

  • crashes could be simply because of these:

    • not having required permission added to the info.plist ( in this case you will get message in the log that permission is missing in .plist file.
    • object are not connected to the correct IBOutlet ( in this case the circle beside the IBOutlet in files would be empty which means its not connected to any object.)

    for your case permission is missing in your info.plist. you should add NSCameraUsageDescription and the value is a string which will be shown on permission dialog when asks user the permission to open camera . it will be like this:

    enter image description here