I have a working implementation of getting a photo from the camera and library but for some reason this one just keeps creating a nil value when try to grab the image. Also, I'm not using the simulator, I'm using my device. I have tried other suggestions on here I've seen but they haven't worked yet. Here is the code for this situation:
cameraMenu = {
let menu = UIAlertController(title: "Add Photos", message: nil, preferredStyle: .actionSheet)
let camera = UIAlertAction(title: "Take Photo", style: .default, handler: { (action) in
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera;
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true, completion: nil)
}
})
let library = UIAlertAction(title: "Choose from Library", style: .default, handler: { (action) in
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)
}
})
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
menu.addAction(camera)
menu.addAction(library)
menu.addAction(cancel)
return menu
}()
and this for the handler
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
imagePost.image = image
let user = Auth.auth().currentUser
var data = Data()
data = UIImageJPEGRepresentation(self.imagePost.image!, 0.8)!
let metaData = StorageMetadata()
metaData.contentType = "image/jpg"
let imageRef = self.storageRef.child("users/\((user?.displayName)!)/profileImage/\((user?.displayName)!)PI.jpg")
imageRef.putData(data, metadata: metaData) { (metaData, error) in
if let error = error {
print(error.localizedDescription)
}else{
//store downloadURL
let downloadURL = metaData!.downloadURL()!.absoluteString
//store downloadURL at database
self.ref.child("users").child((user?.displayName)!).child("profileImageUrl").setValue(downloadURL)
self.newProfileImage = downloadURL
self.currentUser?.profileImageUrl = self.newProfileImage
print("uploaded")
self.dismiss(animated: true, completion: nil)
}
}
} else {
print("something went wrong")
self.dismiss(animated: true, completion: nil)
}
}
I get this error: "fatal error: unexpectedly found nil while unwrapping an Optional value 2017-08-22 15:59:35.458594-0400 Stacks iOS[2439:415896] fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)" as well as this stopping onto a machine code step: "Thread 1: EXC_BREAKPOINT (code=1, subcode=0x101e06644)"