0

I am using parse.com to produce a simple app which retrieves data online and fills my table. I have been successful in populating my table cell textlabels but when I try retrieving images I get shown nothing in my PFTableViewCells.

My code looks as follows:

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject!) -> PFTableViewCell! {
    let cell = tableView.dequeueReusableCellWithIdentifier("reuse", forIndexPath: indexPath) as PFTableViewCell
    var image : UIImage = UIImage(named: "first")!
    cell.textLabel?.text = object.objectForKey("Name") as? String
    var thumbnail:PFFile = object.objectForKey("Image") as PFFile
    cell.imageView?.image = UIImage(named: "first")
    cell.imageView?.file = thumbnail
    cell.imageView?.loadInBackground()
    return cell
}

Is there anything wrong with how I set this up?

Mohammed B
  • 305
  • 1
  • 4
  • 14
  • A lot of people get confused with this. Even though you save images as a PFFile in the backend, you retrieve it as data through code. See my answer [here for reference](http://stackoverflow.com/questions/27595220/downloading-pffile-image-from-parse-append-it-to-array-and-fill-uiimageview-wi/27596932#27596932) – soulshined Jan 03 '15 at 01:32

2 Answers2

0

In Parse PFFile store Image as Binary Data so you can get this in NSData and convert that NSData into Image. I am Showing you code that i have used to get image from parse object in objective C , hope it work for you.

-(void)myParseObject:(PFObject *)obj
{
 //postImageFile is key in which PFFile is stored in parse

[[obj objectForKey:@"postImageFile"] getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
        if (!error) {
            UIImage *image = [UIImage imageWithData:data];
            // image can now be set on a UIImageView

        }
    }];
}
0

I also had trouble with this from the existing examples; here's what worked eventually. When fetching objects from parse:

let thumbnail  = object["toImage"] as PFFile?

Then later, in cellForRowAtIndexPath:

// set thumbnail image if available (must set to placeholder image first!)
cell!.imageView!.image = UIImage(named: "Placeholder")

// get image from parse.com
let thumbView   = PFImageView()

if thumbnail != nil {
    thumbView.file  = thumbnail
    thumbView.loadInBackground({
        (image, error: NSError!) in
        if error == nil {
            cell!.imageView!.image = image
        }
    })
}
Anthony Scott
  • 1,311
  • 12
  • 9