Collin O.
Collin O.

Reputation: 187

Load PFFile Into UIImageView in Swift 2.1/Xcode 7.2

I'm really having a hard time trying to get the image to load from Parse... I have it all set up that it sends the image as a file through JavaScript to Parse (see screenshot) and now I'm trying to load the image into my UIImageView in Swift 2.1/Xcode 7.2 Beta. No errors return and the rest of the app functions like it's supposed to. I cannot seem to get it working, though. Any help would be much appreciated.

let currentUser = PFUser.currentUser()
    if currentUser != nil {

        if let companyImage = currentUser!["pic"] as? PFFile {
            companyImage.getDataInBackgroundWithBlock({ ( imageData: NSData?, error: NSError?) -> Void in
                if error == nil{
                    self.pic.image = UIImage(data: imageData!)
                } else {
                    print("No image found")
                }
            })
        }

Here's a screenshot of what shows up in Parse after I save it through a website I created with JavaScript

Edit:

I have also edited the Info.plist file in my app to make sure that requests made to the internet could be handled, and it worked because I was able to load the user's username.

Upvotes: 1

Views: 228

Answers (1)

kader kabore
kader kabore

Reputation: 345

i don't think the current user contains other object than the default.To be sure make sure you have the user data of the row. so try this

var query = PFUser.query()
query.whereKey("objectId", equalTo: PFUser.currentUser().objectId)
query.findObjectsInBackgroundWithBlock {
  (objects: [PFObject]?, error: NSError?) -> Void in

  if error == nil {
    // The find succeeded.
    print("Successfully retrieved \(objects!.count) scores.")
    // Do something with the found objects
    if let objects = objects {
if let companyImage = objects[0]!["pic"] as? PFFile {
            companyImage.getDataInBackgroundWithBlock({ ( imageData: NSData?, error: NSError?) -> Void in
                if error == nil{
                    self.pic.image = UIImage(data: imageData!)
                } else {
                    print("No image found")
                }
            })

    }
  } else {
    // Log details of the failure
    print("Error: \(error!) \(error!.userInfo)")
  }
}

Upvotes: 0

Related Questions