How to pull file from user class parse swift

828 views Asked by At

In my app, when a user signs up, he/she signs up, an image is added to the user class. The code used to do this is...

    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
    })

Later in my app, I want to pull that picture to put it into a UIImageView. The way I tried to do it was this.

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

However, this returns the error "AnyObject? is not convertible to PFFile". I would like to know how I can retrieve the file with the key "image" from the user class. Thanks for your help.

1

There are 1 answers

5
Mr UziBazooka On

An image is stored in parse as a datafile. To retrieve the image again from parse, and load it to you UIImage. You need to convert the image

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)
  }
}

And change the let image to the image you want to load your image.