CoderMan
CoderMan

Reputation: 182

How to retrieve a image from Parse?

I'm trying to get an image from a parse server that is hosted on Heroku. So far I have gotten the Text values but the images are the only problem.

Here is my code so far:

// Getting Info from servers
    let dataQ = PFQuery(className: "message")
    dataQ.findObjectsInBackground { (objects: [PFObject]?, error: Error?) -> Void in

        if error == nil {
            for object in objects! {
               self.sentFromLabel.text = object["sender"] as? String

                // Get Image from server
                object["Picture"].getDataInBackgroundWithBlock
            }
        } else {
            print(error)
        }
    }

So far when I use .getDataInBackgroundWithBlock it gives me an error. I have no clue why. What is the Swift 3 version to allow me to get an image from the server?

Upvotes: 0

Views: 790

Answers (1)

Cliffordwh
Cliffordwh

Reputation: 1430

Here is a swift 3 example!

@IBOutlet weak var fullImage : UIImageView!

  //place this in your for loop
  let imageFile = (object.object(forKey: "ImageFile") as? PFFile)
      imageFile?.getDataInBackground (block: { (data, error) -> Void in
           if error == nil {
                         if let imageData = data {
                        //Here you can cast it as a UIImage or do what you need to
                             self.fullImage.image = UIImage(data:imageData)
                         }
          }
    })

Upvotes: 2

Related Questions