2015-10-13 37 views
0

我目前有一個tableView可以顯示任意數量的結果。每行都有一個從服務器拉出的圖像。假設服務器速度快並且設備具有可靠的連接,在滾動列表時如何減少滯後。減少使用服務器端圖像的Tableview時的滾動滯後

我看着加載在後臺線程上的圖像,這顯然改善了事情,但是你可以清楚地看到圖像渲染,當你上下滾動。

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

     static NSString *CellIdentifier = @"Cell"; 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

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

     if (tableView == self.searchDisplayController.searchResultsTableView) 
     { 
     cell.textLabel.text = [self.searchResult objectAtIndex:indexPath.row]; 
     } 
     else 
     { 
     cell.textLabel.text = self.TableData[indexPath.row]; 
     } 
     _ImageURL = self.ImageData[indexPath.row]; 
     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 
     dispatch_async(queue,^
     { 
      UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_ImageURL]]]; 
      dispatch_sync(dispatch_get_main_queue(),^
      { 

       [[cell imageView] setImage:image]; 
       [cell setNeedsLayout]; 
      }); 
     }); 
     return cell; 
} 

有什麼方法可以改善這種情況,還是更有可能成爲服務器速度和設備帶寬的限制。我的服務器不是Facebook服務器,但它是我用於其他應用程序的體面付費應用程序。

此外,由於將圖像移動到後臺任務,我注意到一次又一行會錯過加載圖像,對此有什麼想法?

+0

你在做什麼是完全錯誤的。您必須提供佔位符並立即返回。稍後,您可以在有圖像時重新加載表格。請做一個搜索,因爲這是所有問題中最常見的問題之一。 – matt

回答

3

我建議你使用SDWebImage庫,從異步下載到緩存管理,一切都將爲你處理。 這裏是 the link下載和文檔。

如果您不需要圖像緩存,也可以使用Facebook的 AsyncDisplayKit

祝你好運。