尝试设置 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 的个人资料图片时出现致命错误的主要内容,如果未能解决你的问题,请参考以下文章
在 quickblox 上上传个人资料图片时 Swift 出错