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!