我正在尝试设置注册页面,以便用户可以设置个人资料图片但是只要我按下注册按钮并且它会转到注册页面,它会因为个人资料图片代码而一直崩溃。这是设置个人资料照片的按钮
@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
}
...
}
不断崩溃的行是让profilePicture ....行给出错误: 致命错误:在展开Optional值时意外发现nil (lldb)
答案 0 :(得分:0)
在展开时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
}