2015-09-23 65 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」它將幫助您顯示活動指示器,直到圖像加載完成。 –