George Sabanov
George Sabanov

Reputation: 189

Loading photos in uitableview asynchronously

In my app I load cell's photo right in cellforrowatindexpath, but it takes main thread for a long period, and when I scroll, my app stops for a while. What should I do? If I add async thread it would call it every time I scroll and there could be 2 or more equal photo loading requests, and this is not good... Is there any idea without spaghetti coding?

Upvotes: 0

Views: 47

Answers (1)

DHEERAJ
DHEERAJ

Reputation: 1468

Try SDWebImage framework

https://github.com/rs/SDWebImage

Sample Code

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"MyIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:MyIdentifier];
    }

    // Here we use the new provided sd_setImageWithURL: method to load the web image
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"Get each url from array and set it here using indexpath.row"]
                      placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

    cell.textLabel.text = @"My Text";
    return cell;
}

It supports variety of features like caching etc.

Upvotes: 2

Related Questions