2013-09-30 24 views
1

我有一個應用程序,我加載了很多大型圖像。當我懶加載它們,甚至在圖像加載後,單元格不會加載它們,直到我將手指從屏幕上移開。我打電話給我的downloadImageForVisiblePaths功能在UIScrollViewDelegate方法scrollViewDidEndDragging並從該scrollViewDidEndDecelerating之餘,我也設置在圖像中UITableViewcellForRowAtIndexPath方法,像這樣:UITableView懶惰加載的圖像不加載,直到手指關閉

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

//  Code to load reusable custom cell 

CustomObject *object = (CustomObject*) [self.tableArray objectAtIndex: indexPath]; 

if(!object.mainImage){ 

    [self downloadImageForIndexPath:indexPath]; 

    cell.mainImageView.image = [UIImage imageNamed:@"placeholder"]; 

}else{ 

    cell.mainImageView.image = object.mainImage; 

} 

    return cell; 
} 

downloadImageForIndexPath看起來是這樣的:

-(void) downloadImageForIndexPath:(NSIndexPath*) indexPath{ 

    UIImage *loadedImage = [[UIImage alloc] init]; 

    // take url and download image with dispatch_async 

    // Once the load is done, the following is done 

    CustomCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 

    cell.mainImageView.image = loadedImage; 

    CustomObject *object = (CustomObject*) [self.tableArray objectAtIndex: indexPath]; 

    object.mainImage = loadedImage; 

    dispatch_async(dispatch_get_main_queue(), ^{ 
      [self.tableVIew reloadData]; 
     }); 
} 

我看不到我要去哪裏錯了。即使手指在屏幕上,我也需要加載圖像。這種行爲類似於圖片在Google+,Instagram或Facebook等應用上的加載方式。

任何指針將不勝感激。

回答

2

很難說,因爲您沒有包含downloadImageForIndexPath的所有代碼,但它看起來像是從後臺線程(不應該觸摸後臺線程中的UI控件)將圖像分配給單元格。另外,如果您直接更新單元格,則不需要撥打reloadData

我也建議使用SDWebImage在桌面視圖中顯示遠程圖像。

+0

嗯,我實際上是在主線程上設置圖像,只在後臺線程中獲取圖像。我正在嘗試SDWebImage。會告訴你這件事的進展的。 –

+0

SDWebImage完美地工作。一個問題,但。當我嘗試發佈應用程序時,蘋果不會給我關於使用非公共選擇器的警告嗎? –

+1

SDWebImage中的所有內容都是公開的,應用商店中有很多應用程序正在使用它。 – iRaivis