George Sabanov George Sabanov - 16 days ago 4x
iOS Question

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?


Try SDWebImage framework

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

    // 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.