I was trying to set up the sign up page so that the user can set up a profile picture but as soon as i press the sign up button and it segues to the signup page it keeps crashing because of the profile picture codes. This is the button to set the profile picture
@IBAction func setProfilePicture(sender: AnyObject) {
let myPickerController = UIImagePickerController()
myPickerController.delegate = self
myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(myPickerController, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
profilePictureIV.image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.dismissViewControllerAnimated(true, completion: nil)
}
and this is the code to send the data to parse in the viewDidLoad()
method
let newUser = PFUser()
let profilePicture = UIImageJPEGRepresentation((profilePictureIV?.image)!, 1)
if(profilePicture != nil) {
let profilePictureImageFile = PFFile(data: profilePicture!)
newUser["profilePicture"] = profilePictureImageFile
}
...
}
The line that keeps crashing is the let profilePicture.... line giving an error: fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)
The error occurs when profilePictureIV?.image
is nil while being unwrapped, so check it
let newUser = PFUser()
if let profilePicture = UIImageJPEGRepresentation(profilePictureIV?.image, 1) {
let profilePictureImageFile = PFFile(data: profilePicture)
newUser["profilePicture"] = profilePictureImageFile
}
or
let newUser = PFUser()
if let profilePictureImage = profilePictureIV?.image {
let profilePicture = UIImageJPEGRepresentation(profilePictureImage, 1)!
let profilePictureImageFile = PFFile(data: profilePicture)
newUser["profilePicture"] = profilePictureImageFile
}