2

在我的应用程序中,当用户注册时,他/她注册,图像被添加到用户类。用于执行此操作的代码是...

    var newUser = PFUser()
    let imageData = UIImagePNGRepresentation(self.imageView.image)
    let imageFile = PFFile(data: imageData)
    newUser.setObject(imageFile, forKey: "image")
    newUser.signUpInBackgroundWithBlock({
        (success, error) -> Void in
    })

稍后在我的应用程序中,我想将那张图片拉到 UIImageView 中。我尝试这样做的方式是这样的。

    var user = PFUser.currentUser()  //Error on this line
    let profileImage = user["image"] as! PFFile

但是,这会返回错误“AnyObject?不可转换为 PFFile”。我想知道如何从用户类中检索带有“图像”键的文件。谢谢你的帮助。

4

1 回答 1

1

图像作为数据文件存储在 parse 中。从解析中再次检索图像,并将其加载到 UIImage。您需要转换图像

var user = PFUser.currentUser()
let userImageFile = user["image"] as PFFile
userImageFile.getDataInBackgroundWithBlock {
(imageData: NSData!, error: NSError!) -> Void in
if !error {
    let image = UIImage(data:imageData)
  }
}

并将 更改为let image您要加载图像的图像。

于 2015-06-11T19:13:39.867 回答