Unable to Convert PFFile to UIImage

822 views Asked by At

I am wondering if I have done anything wrong here.

I have a subclass of PFUser which has a property of profileImage, which is a PFFile

There are some cases when I don't save the profileImage right away to Parse and I only pin it to the localDatastore.

But when I tried to retrieve it back from the localDataStore and use the getDataInBackgroundWithBlock. It does not return any error, but the NSData returned by the callback is always nil.

if let profileImage = PGUser.currentUser()?.profileImage {
    profileImage.getDataInBackgroundWithBlock { (data: NSData?, error: NSError?) -> Void in
        if error == nil {
            if  data != nil {
                println("IMAGE DATA FOUND")
                let image = UIImage(data: data!);
                self.profileImageView.image = image;
            }
            else {
                //DATA IS ALWAYS NIL    
                println("NO IMAGE DATA FOUND")
            }
        }
    }

}
  1. The PGUser.currentUser()?.profileImage is NOT NIL
  2. No error returned by the getDataInBackgroundWithBlock function.
  3. BUT the data is always NIL.

Any thoughts on what am I doing wrong?

Thank you!!

1

There are 1 answers

1
iAnurag On

Try this. I solved my problem with this

 let profileImage = userPhoto["imageFile"] as PFFile
userImageFile.getDataInBackgroundWithBlock {
(imageData: NSData!, error: NSError!) -> Void in
if !error {
    let image = UIImage(data:imageData)
self.profileImageView.image = image
 }
}