When Launching Camera App Crashes

I am trying to access the camera to take pictures in my app. Here is my code:


@IBAction func uploadPicture(_ sender: Any) { 
        imagePickerController = UIImagePickerController() 
        imagePickerController.delegate = self 
        imagePickerController.sourceType = .camera 
        present(imagePickerController, animated: true, completion: nil) 
    } 
    func saveImage(imageName: String){ 
        //create an instance of the FileManager 
        let fileManager = FileManager.default 
        //get the image path 
        let imagePath = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent(imageName) 
        //get the image we took with camera 
        if pic4 != nil { 
            let image = pic4 
            //get the PNG data for this image 
            let data = image.jpegData(compressionQuality: 1) 
            //store it in the document directory 
            fileManager.createFile(atPath: imagePath as String, contents: data, attributes: nil) 
//                        print("Big Bobs\(image.imageOrientation.rawValue)") 
            if image.size.height > image.size.width { 
                newWine.isPortrait = true 
            } 
            print("Bigmacs \(image.imageOrientation.rawValue)") 
        } 
    } 
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { 
//// Local variable inserted by Swift 4.2 migrator. 
//let info = convertFromUIImagePickerControllerInfoKeyDictionary(info) 
// 
// 
//       if let image2 = info[convertFromUIImagePickerControllerInfoKey(UIImagePickerController.InfoKey.originalImage)] as? UIImage { 
// 
//        newWine.picture = image2 
//        // 
//      } 
// 
//        self.dismiss(animated: true, completion: nil) 
        imagePickerController.dismiss(animated: true, completion: nil) 
     // makes image show in image view if I want 
        pic4 = (info[UIImagePickerController.InfoKey.originalImage] as? UIImage)! 
//        imageView.image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage 
        SVProgressHUD.showSuccess(withStatus: "Picture Added Successfully") 
        newWine.pictureIncluded = true 
    }

However when I press the upLoadPicture button the app crashes. Everything works fine when using the .photoLibrary in line 5.


My info.plist includes the following:


Privacy - Photo Library Additions Usage Description

Privacy - Photo Library Usage Description

Privacy  -  Camera Usage Description




Thanks to all help!

Accepted Reply

It was a problem with info.plist. Problem fixed

Replies

Were you using a simulator? I believe it crashes on a simulator.

I was using actual iPhone.

Where exactly do you crash ?


Try to define mediaType as well and allow editing


@IBAction func uploadPicture(_ sender: Any) {
        let sourceType = [UIImagePickerController.SourceType.camera]
        if !UIImagePickerController.isSourceTypeAvailable(sourceType) {          // Take note of !  at the beginning
            print("no camera")
            return
        }

        if let mediaTypes = UIImagePickerController.availableMediaTypes(for: sourceType) {
             if UIImagePickerController.isSourceTypeAvailable(sourceType) && mediaTypes.count > 0 {
                 imagePickerController = UIImagePickerController
                 imagePickerController.mediaTypes = mediaTypes 
                 imagePickerController.delegate = self
                 imagePickerController.allowsEditing = true     // sometimes crashes without it
                 imagePickerController.sourceType = sourceType
                 present(imagePickerController, animated: true, completion: nil)
             }
       }
}

It was a problem with info.plist. Problem fixed

May I ask what did you add in info.plist?