如何从用户类中解析文件解析swift

时间:2015-06-11 18:59:23

标签: ios swift parse-platform pfuser pffile

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

    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"。我想知道如何使用键" image"来检索文件。来自用户类。谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

图像作为数据文件存储在解析中。要从解析中再次检索图像,并将其加载到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更改为您要加载图片的图片。