rckehoe
rckehoe

Reputation: 1198

My image from Parse.com isn't showing.. Using UIImage & UIImageView with PFFile

I have a table on Parse.com that allows me to store Event information. Image, title, date and description. I am trying to obtain the image and assign it to a UIImageView within a custom cell that I have developed... The data loads fine, and the custom cell works, but the image doesn't ever update... The logic in my code seems legit, but then again, I am new... So any help is appreciated.

The problematic code is right after where I have the comment "//--- Event Image"

I am simply trying to figure out why my image isn't updating.

Thanks!

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
 {

static NSString *simpleTableIdentifier = @"eventCell";

VFTEventCell *cell = (VFTEventCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil)
{

    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"EventTableCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];

}

//--- Event Title
cell.title.text = [object objectForKey:@"title"];

//--- Event Date
NSDate *eventDate = [object objectForKey:@"date"];

if(eventDate != NULL)
{

    NSDate *date = [object objectForKey:@"date"];

    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM/dd/yyyy"];
    NSString *dateString = [dateFormatter stringFromDate:date];

    cell.date.text = dateString;

}

//--- Event Image
PFFile *eventImage = [object objectForKey:@"image"];

if(eventImage != NULL)
{

    [eventImage getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {

        UIImage *thumbnailImage = [UIImage imageWithData:imageData];
        UIImageView *thumbnailImageView = [[UIImageView alloc] initWithImage:thumbnailImage];

        cell.image = thumbnailImageView;

    }];

     }

     return cell;

 }

Upvotes: 1

Views: 1253

Answers (2)

Mohannad A. Hassan
Mohannad A. Hassan

Reputation: 1648

You're trying to set this in a background thread. You can only access UIKit from the main thread.

[eventImage getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {

        UIImage *thumbnailImage = [UIImage imageWithData:imageData];
        dispatch_async(dispatch_get_main_queue(), ^{
            cell.imageView.image = thumbnailImage;
        });

    }];

Note: Consider using PFImageView.

Upvotes: 1

rckehoe
rckehoe

Reputation: 1198

I figured out my problem shortly after my post... For anyone who gets stuck. I simply removed the creation of a UIIMageView. Makes sense considering in my XIB file, I am already creating the imageView... But, I set that object using setImage.

here is the code.

 //--- Event Image
PFFile *eventImage = [object objectForKey:@"image"];

if(eventImage != NULL)
{

    [eventImage getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error)
    {

        UIImage *thumbnailImage = [UIImage imageWithData:imageData];

        [cell.image setImage:thumbnailImage];

    }];

}

Upvotes: 1

Related Questions