2015-09-23 62 views
0

在我的应用程序中,我在cellforrowatindexpath中加载单元格的照片,但它需要长时间的主线程,而当我滚动时,我的应用程序停止了一段时间。 我该怎么办?如果我添加异步线程,它会在每次滚动时调用它,并且可能有2个或更多相同的照片加载请求,这不是很好......没有任何想法没有意大利面条编码?异步加载在uitableview中的照片

+0

写作 '在表视图iOS的异步图像加载'谷歌会给你指针为你的问题。 –

回答

2

尝试SDWebImage框架

https://github.com/rs/SDWebImage

示例代码

- (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; 
} 

它支持多种像缓存等特征

+0

使用“https://github.com/JJSaccolo/UIActivityIndi​​cator-for-SDWebImage”它将帮助您显示活动指示器,直到图像加载完成。 –