尝试设置 PFUser 的个人资料图片时出现致命错误

Posted

技术标签:

【中文标题】尝试设置 PFUser 的个人资料图片时出现致命错误【英文标题】:Getting a fatal error when trying to set up PFUser's profile picture 【发布时间】:2015-09-29 13:11:55 【问题描述】:

我试图设置注册页面,以便用户可以设置个人资料图片,但是一旦我按下注册按钮并转到注册页面,它就会因为个人资料图片代码而不断崩溃。这是设置头像的按钮

    @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.... 行给出错误: 致命错误:在展开可选值时意外发现 nil (lldb)

【问题讨论】:

【参考方案1】:

profilePictureIV?.image在解包时为nil时出现错误,请检查一下

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

【讨论】:

不行,还是不行。它给了我一个错误来修复它并将profilePictureIV?.image替换为(profilePictureIV?.image)! 哦耶!非常感谢!!

以上是关于尝试设置 PFUser 的个人资料图片时出现致命错误的主要内容,如果未能解决你的问题,请参考以下文章

不断出现致命错误:在展开可选值时意外发现 nil

在 quickblox 上上传个人资料图片时 Swift 出错

注册后向 PFUser 添加列?解析,斯威夫特

使用两个PFUser对象查询对象时,如何更新Parse中的值?

解析以下 PFUser

为啥 PHP 错误会打印两次?