2012-01-27 97 views
1

我已經足夠了解我應該使用SDWebImage在遠程圖像中填充我的UITableView中的縮略圖。UITableView遠程縮略圖圖像,帶佔位符本地圖像

我的問題是,如何在每個單元格中顯示單元格後,SDWebImage完成加載之前立即顯示默認縮略圖?

我想在沒有可用的遠程圖像的情況下也應該顯示默認縮略圖。

意見/建議?

感謝所有

+0

爲什麼不只是添加您的默認圖像,然後當您的其他圖像下載繪製在上面? – ader 2012-01-27 17:18:43

回答

1

最初,您可以將所有單元格設置爲具有默認圖像。然後您可以使用回調代理來更新所需的單元格,以便在加載圖像後生成該圖像。我目前使用JMImageCache進行遠程圖像處理,這非常簡單。

首先,將JMImageCache.h和JMImageCache.m文件添加到您的項目中。假設您爲名爲CustomCell的表使用自定義單元格,該單元格在ImageViewer中保存了名爲imageViewer的圖像,請在CustomCell.h中導入JMImageCache.h文件。更新您的CustomCell接口,使其成爲JMImageCacheDelegate,還加一個NSString IMAGE_URL場:

@interface CustomCell : UITableViewCell <JMImageCacheDelegate> 
{ 
NSString *image_url; 
... 
} 

// Also set image_url property fields so that it can be accessed from other files 

然後,我們需要處理時,圖像下載完成,使細胞的圖像將被更新。合成image_url並將以下內容添加到CustomCell.m。

- (void) cache:(JMImageCache *)c didDownloadImage:(UIImage *)i forURL:(NSString *)url 
{ 
if ([url isEqualToString:image_url]) { 
    [self.imageViewer setImage:i]; 
    [self setNeedsLayout]; 
} 
} 

然後在有你的tableView在指數路徑功能的單元格行的文件:

CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:@"CustomCell"]; 
if (cell == nil) { 
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil]; 
    for (id currentObject in topLevelObjects) { 
     if ([currentObject isKindOfClass:[UITableViewCell class]]) { 
      cell = (CustomCell *)currentObject; 
      break; 
     } 
    } 
} 

[cell.imageViewer setImage:[UIImage imageNamed:"default.png"]]; 

//Code for getting string of URL you want for current cell called 'cell_image' 

[cell setImage_url:cell_image]; 
[cell.imageViewer setImage:[[JMImageCache sharedCache] imageForURL:image_url delegate:cell]]; 

JMImageCache對github上一個簡單的例子,如果你需要更多的援助。

1

有一個真正的好官例如從蘋果約懶加載的圖像:

http://developer.apple.com/library/ios/#samplecode/LazyTableImages/index.html

的想法很簡單,首先將因爲沒有下載和啓動您的佔位符圖像下載該字段的圖像,當它完成時,回調將被調用到您的tableViewController,並重新加載相關索引路徑中的相關單元格。如果下載失敗,回調永遠不會被調用,您的佔位符圖像將永遠存在。