Получение фатальной ошибки при попытке настроить изображение профиля PFUser
Я пытался настроить страницу регистрации, чтобы пользователь мог настроить изображение профиля, но как только я нажимаю кнопку регистрации, и она переходит на страницу регистрации, происходит сбой из-за кодов изображения профиля. Это кнопка для установки картинки профиля
@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)
}
и это код для отправки данных для анализа в viewDidLoad()
метод
let newUser = PFUser()
let profilePicture = UIImageJPEGRepresentation((profilePictureIV?.image)!, 1)
if(profilePicture != nil) {
let profilePictureImageFile = PFFile(data: profilePicture!)
newUser["profilePicture"] = profilePictureImageFile
}
...
}
Строка, которая продолжает сбой, - это строка let profilePicture...., сообщающая об ошибке: фатальная ошибка: неожиданно найден ноль при развертывании необязательного значения (lldb)
1 ответ
Решение
Ошибка возникает, когда profilePictureIV?.image
при развертывании это ноль, так что проверьте его
let newUser = PFUser()
if let profilePicture = UIImageJPEGRepresentation(profilePictureIV?.image, 1) {
let profilePictureImageFile = PFFile(data: profilePicture)
newUser["profilePicture"] = profilePictureImageFile
}
или же
let newUser = PFUser()
if let profilePictureImage = profilePictureIV?.image {
let profilePicture = UIImageJPEGRepresentation(profilePictureImage, 1)!
let profilePictureImageFile = PFFile(data: profilePicture)
newUser["profilePicture"] = profilePictureImageFile
}