0

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

    @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)时意外发现 nil

4

1 回答 1

0

解包时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
}
于 2015-09-29T13:18:17.927 回答