2

I have a UITableView that will contain a high number of images.

Each cell will have 5 images, that are loaded randomly from Parse.com.

My query code, right now, is in cellForRowAtIndexPath.

This causes a few issues:

1.) There's some lag/delay when scrolling the table.

2.) The images, now for some odd reason, are not showing in the proper location.

3.) The images reload/change every time the table is scrolled.

I'd like for the images to be "loaded" just once when on this UIView, in the correct place, and with smooth scrolling.

What's the best way to handle this?

Here's the code for how I am loading the images:

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

    // set up the cell here

    // query and display random images from parse.com
    PFQuery * query = [PFUser query];
    [query whereKey:@"category" equalTo:self.cell.label.text];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

        if (error)
        {
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
        else
        {
            for (int i = 0; i < objects.count; i++)
            {
                self.profilePicObject = [objects objectAtIndex:i];

                int imgRandom = arc4random() % [objects count];

                self.randomProfilePicObject = [objects objectAtIndex:imgRandom];

                self.imageFile = [self.randomProfilePicObject objectForKey:@"imageFile"];
                NSURL * imageFileURL = [[NSURL alloc] initWithString:self.imageFile.url];
                NSData * imageData = [NSData dataWithContentsOfURL:imageFileURL];
                UIImage * aImage = [[UIImage alloc] initWithData:imageData];

                self.cell.profilePicOne.image = aImage;
            }
    }

    return self.cell;

}

EDIT:

I'm using SDWebImage and the table now scrolls smoothly and images are displaying.

The new issue:

Images are loading in wrong cells.

I'm querying the table and matching the label text to the proper string in my Parse table and using that to display the proper image.

Initially, some images load in the correct cell and some do not.

When I scroll, images appear all over the place.

What's the best way to fix this?

Here is how I am setting up the cells:

static NSString * cellIdentifier = @"FilterSelectCell";

self.cell = (FilterSelectCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (self.cell == nil)
    {
        NSArray * nib = [[NSBundle mainBundle] loadNibNamed:@"FilterSelectCell" owner:self options:nil];
        self.cell = [nib objectAtIndex:0];

        self.cell.profilePicOne.clipsToBounds = YES;
        self.cell.profilePicOne.layer.cornerRadius = 20.0;

        self.cell.profilePicTwo.clipsToBounds = YES;
        self.cell.profilePicTwo.layer.cornerRadius = 20.0;

        self.cell.profilePicThree.clipsToBounds = YES;
        self.cell.profilePicThree.layer.cornerRadius = 20.0;

        self.cell.profilePicFour.clipsToBounds = YES;
        self.cell.profilePicFour.layer.cornerRadius = 20.0;

        self.cell.profilePicFive.clipsToBounds = YES;
        self.cell.profilePicFive.layer.cornerRadius = 20.0;
    }

    self.cell.label.text = [self.myArray objectAtIndex:indexPath.row];
Luke Irvin
  • 1,179
  • 1
  • 20
  • 39
  • You can preload some random images, however many you think will be needed in -viewDidLoad. Why defer that until when they're needed? Additionally, you can (and pretty much have to) download the images in the background, using something like NSURLConnection or SDWebImage. – Josh Jan 03 '14 at 03:13

4 Answers4

1

The best solution would be to use an image caching system such as SDWebImage to prevent duplicate web queries for the same image. It is extremely easy to use and lets you use a placeholder loading image while downloading the image data from the URL. It also loads images asynchronously so your UI isn't blocked and it will release images from the cache before your application is overloaded. You simply load images like this and SDWebImage does the magic:

[self.cell.profilePicOne setImageWithURL:imageFileURL placeholderImage:[UIImage imageNamed:@"LOADING_IMAGE.png"]];

SDWebImage Repository

Mike S
  • 11,329
  • 6
  • 41
  • 76
  • Getting this error with this: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImageView setImageWithURL:placeholderImage:]: unrecognized selector sent to instance – Luke Irvin Jan 03 '14 at 05:35
  • Did you import the SDWebImage framework that I linked into your project? There are installation instructions in the link at the bottom. If so then add this line at the top of the file you're trying to use it in: #import "SDWebImage/UIImageView+WebCache.h" – Mike S Jan 03 '14 at 05:39
  • Yes. When I add the -ObjC it causes even more issues. – Luke Irvin Jan 03 '14 at 05:45
  • Does this help? http://stackoverflow.com/questions/12306161/build-fail-when-using-sdwebimage – Mike S Jan 03 '14 at 05:55
  • Ok. I got it working. New issue. Images are loading all over the place. I'm checking to see if a string in Parse matches the label text on the cell, if yes, display proper image. but the images are loading in wrong cells as I scroll. – Luke Irvin Jan 03 '14 at 05:55
  • I'm not sure what would cause that offhand. Its possible there is an issue with the way the cells are instantiated or dequeued, or perhaps there is some defect with the how you are comparing your Parse response with the label text? – Mike S Jan 03 '14 at 05:59
0

Lazy loading is the better solution for this take a look on the following discussion Lazy loading UITableView with multiple images in each cell

Community
  • 1
  • 1
LML
  • 1,659
  • 12
  • 29
0

Use Asynchronous image downloader with cache support with an UIImageView category.I have been using this for long time.Damn sure ur problem may overcome with this SDWebImage

0

You can checkout this utility APSmartStorage. It supports downloading, memory and disk cache.

Krivoblotsky
  • 1,492
  • 11
  • 17