Tim
Tim

Reputation: 225

How do I load an image from parse in swift?

I was wondering if anyone can help me, I'm new to app developing I am uploading images from my app to parse with no problem with help from parse documentation.

    let imageData = UIImagePNGRepresentation(scaledImage)
    let imageFile: PFFile = PFFile(data: imageData)

    var userPhoto = PFObject(className: "postString")
    userPhoto["imageFile"] = imageFile
    userPhoto.saveInBackground()

but when i add the code to retrieve the image back, I'm a bit lost :/

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

where is the "anotherPhoto" coming from ? Parse did say "Here we retrieve the image file off another UserPhoto named anotherPhoto:"

Upvotes: 6

Views: 8245

Answers (1)

Nick
Nick

Reputation: 9860

anotherPhoto would be an instance of your postString (userPhoto in your upload example). The file downloading example that would work for you is like this:

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

Any PFFile must be referenced from a normal PFObject or it cannot be retrieved. PFFile objects themselves will not show up in the data browser.

Upvotes: 5

Related Questions