Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:"

like image 380
Tim Avatar asked Jul 12 '14 21:07

Tim


People also ask

How to download image from server in Swift?

Open ViewController. swift and create an outlet with name imageView of type UIImageView! , an implicitly unwrapped optional. The idea is simple. The view controller downloads an image from a URL and displays it in its image view.

How do I assign an image in Swift?

First one is to drag and drop an image file from Finder onto the assets catalog. Dragging the image will automatically create new image set for you. Drag and drop image onto Xcode's assets catalog. Or, click on a plus button at the very bottom of the Assets navigator view and then select “New Image Set”.

What is UIImage in Swift?

An object that manages image data in your app.


1 Answers

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.

like image 151
Nick Avatar answered Oct 14 '22 12:10

Nick