1

I've got an image in Parse that I want to load as the image for a button.

Here's my code:

    let myData = PFObject(className: "Headliner")
    if let theImageFileINeed = myData["ImageFile"]  as! PFFile? {
        theImageFileINeed.getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in
            if (error == nil) {
                print("loadingimage?")
                if let imageData = imageData {
                    let image = UIImage(data: imageData)
                    self.headlinerImage.setImage(image, forState: UIControlState.Normal)
                }
            } else {
                print("error")
            }
        }
    }

Here's the code I'm referencing from the Parse documentation:

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

When I use that exact code (I'm putting this in viewDidLoad, but am not sure if that's correct), swapping out the name of my table for "anotherPhoto" in the example (imageFile is the name of my field, too, so I didn't have to change that), I get the following error message: "Use of unresolved identifier "Headliner". So, then I assumed that maybe this goes inside a query? Or I need to specify the table somehow I want to pull data from, so I added the myData variable to pull that in.

When I run this, I don't get an error message, but my button doesn't update the image from parse.

I suspect it is related to types, probably in that "let my data = PFObject(className: "headliner") line... But I don't know how to fix it...

Any help would be appreciated! I bake cookies, so I'll send you some if you help me fix this!!!

Mali

rici
  • 234,347
  • 28
  • 237
  • 341
  • 1
    `anotherPhoto` is not a table but a PFObject already retrieved from the Parse table. `let myData = PFObject(className: "Headliner")` is just creatind an empty PFObject without any data. Show code for original error... – kRiZ Sep 28 '15 at 22:19
  • thanks so much for the help. That really helps a lot. :]) – Mali LeDoux Phares Sep 30 '15 at 22:03

3 Answers3

1

Pretty sure this code should work. You may have to change the button settings in the interface builder to 'Custom'

enter image description here

Or maybe just create the button programmatically... See here: How to create a button programmatically?

Community
  • 1
  • 1
Brian Daneshgar
  • 343
  • 3
  • 20
  • Thanks so much for responding. I tried your suggestion but with no success :( Thanks again for answering! I really appreciate the input! I tried doing it with and without creating the button programmatically, and the button was created just fine, but it still doesn't show my image... – Mali LeDoux Phares Sep 30 '15 at 21:06
1

Load image in tableView from Parse using PFFile

First step, make sure you import parse library:

import Parse

second step, declare a PFFile array, something like that:

var imageFiles = [PFFile]()

third, store all the images in the array:

let query = PFQuery(className:"your_class")

query.findObjectsInBackgroundWithBlock {
        (objects: [PFObject]?, error: NSError?) -> Void in

        if error == nil{
            for writerData in objects! {
                self.imageFiles.append(writerData["avatar"] as! PFFile)
            }
               /*** reload the table ***/
            self.yourTableView.reloadData()
        } else {
            print(error)
        }
    }

Fourth, in order to show it (in my case I am displaying the images in UITableView), so in cellForRowAtIndexPath:

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! YourClassTableViewCell


     imageFiles[indexPath.row].getDataInBackgroundWithBlock{
            (imageData: NSData?, error: NSError?) -> Void in

            if imageData != nil{
                let image = UIImage(data: imageData!)
                cell.cellAvatarImage.image = image
            } else {
                print(error)
            }
        }
Gilad Brunfman
  • 3,452
  • 1
  • 29
  • 29
0

You need to try update your image in a different thread. ( This got me many times too) Also I generally change the name unwrapped version of my variables so I can distinguish them easily.

    dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    //Image update code goes here
 if let unWrappedimageData = imageData {
                    let image = UIImage(data: unWrappedimageData)
                    self.headlinerImage.setImage(unWrappedimageData, forState: UIControlState.Normal)
                }
                })
UKDataGeek
  • 6,338
  • 9
  • 46
  • 63
  • By "in a different thread" do you mean outside of my query? I'm sorry that my question is so basic! :) If i move it outside of my query, then I get "use of unresolved identifier 'image'"... – Mali LeDoux Phares Sep 30 '15 at 21:05
  • And then when I declare as follows: var imageData : UIImage? I get the following: "cannot invoke initializer for type UIImage" – Mali LeDoux Phares Sep 30 '15 at 21:56