【问题标题】:Issues retrieving PFFile from Parse从 Parse 检索 PFFile 的问题
【发布时间】:2015-08-27 12:05:13
【问题描述】:

我正在创建一个 parse 应用程序,其中用户可以在注册时选择个人资料图片。这是它的代码。

    var profilePictures = PFObject(className: "ProfilePictures")
    let imageData = UIImagePNGRepresentation(self.profileImage.image)
    let imageFile = PFFile(name:"image.png", data:imageData)

    profilePictures["profilePhoto"] = imageFile
    profilePictures["user"] = usernameField.text
    profilePictures.save()

稍后我有一个屏幕,其中 UIImageView 需要填充所选的个人资料图片。

这一直有效,直到应用程序本身完全停止并重新启动。

PFFile 然后被发现为 nil,我收到错误“在展开可选值时意外发现 nil”。

这是显示图片的代码。

     override func viewDidAppear(animated: Bool) {
    var query = PFQuery(className: "ProfilePictures")
    query.whereKey("user", equalTo: PFUser.currentUser()?.username)
    query.findObjectsInBackgroundWithBlock({
        (success, error) -> Void in
        let userImageFile = profilePictures["profilePhoto"] as! PFFile  
        //error is on the above line

        userImageFile.getDataInBackgroundWithBlock({
            (imageData: NSData?, error) -> Void in
            var image = UIImage(data: imageData!)
            self.profileImage.image = image
        })


    })

}

【问题讨论】:

  • 我有同样的问题。你修好了吗?

标签: ios swift parse-platform pffile


【解决方案1】:

由于某种原因,您没有正确设置 userImageFile。它似乎是零。我会检查 Parse 控制台以确认您在 PFile 中有图像。在任何情况下,使用“if let”来避免展开问题可能会更聪明。如果没有保存 PFile,这将无法解决问题,因为如下所述,您应该使用 saveInBackground 并使用通知来确认您已准备好进行检索。

if let userImageFile = profilePictures["profilePhoto"] as! PFFile  {
    //error is on the above line

    userImageFile.getDataInBackgroundWithBlock({
        (imageData: NSData?, error) -> Void in
        var image = UIImage(data: imageData!)
        self.profileImage.image = image
    })
}

【讨论】:

    【解决方案2】:

    您的错误可能在于保存:

    let imageFile = PFFile(name:"image.png", data:imageData)
    profilePictures["profilePhoto"] = imageFile
    profilePictures.save()
    

    您正在保存一个带有指向新的未保存 PFFile 的指针的对象,这会导致错误。你应该先做imageFile.saveInBackground,并使用回调将imageFile分配给profilePictures,然后保存profilePictures。

    您可以通过在 Parse 的数据存储中查看您的 profilePictures 对象上的键“profilePhoto”没有值来确认这一点

    【讨论】:

    • 我同意这个评估。
    • 我已确认问题在于检索图片。该文件在创建用户时保存到控制台中。我试图检查 imageData 是否为零,但似乎问题出在 PFObject 键中。我检查以确保密钥存在,并且确实存在。 @SyedTariq
    猜你喜欢
    • 1970-01-01
    • 2014-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多