2012-03-23 111 views
2

我正在將圖片加載到與單元格文本對應的表格視圖中,因此每個圖像都不相同。隨着用戶向下滾動,iOS必須手動重新加載SSD中的每個圖像,因此滾動非常不連貫。如何緩存圖像或防止需要重新創建表格視圖單元格?其他人通過使用imageNamed:解決了他們的問題,因爲iOS會自動緩存您的圖像,但我從文檔目錄加載圖像,而不是應用程序包。我該怎麼辦?謝謝。在UITableView中緩存圖像

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 

    // Return the number of sections. 
    return 1; 

} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    // Return the number of rows in the section. 
    return [issues count]; 

} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 

     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 

    } 


    // Set up the cell... 
    NSDictionary *dic = [self.issues objectAtIndex:indexPath.row]; 

    cell.text = [dic objectForKey:@"Date"]; 

    cell.imageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/issues/%@/cover.png", documentsDirectory, [dic objectForKey:@"Directory Name"]]]; 

    return cell; 

} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 

     return 150; 

} 
+0

結帳UIImageLoader。它正是你想要的,只有兩個文件要添加到Xcode。這也是非常容易理解的,回購中有兩個很棒的樣本。 https://github.com/gngrwzrd/UIImageLoader – gngrwzrd 2015-12-25 20:36:24

回答

1

隨着緩存,你也可以考慮使用Grand central dispatch在後臺加載圖像。當單元格被加載時,放入一個UIActivityIndi​​cator,然後用一個單獨的線程中的圖像替換它。

也會檢出圖像口吃此相關的答案: Non-lazy image loading in iOS

+1

非常感謝。我正在查看Grand Central Dispatch,並在這裏找到了一個很好的示例項目(https://github.com/SlaunchaMan/GCDExample) – 2012-03-23 23:04:18

4

字典的陣列是你的模型,所以它會是一個自然的地方。棘手的部分是讓你的模型變化。你可以讓你的模型在課堂上變化,或者在緩存圖像時跳過這些環節。我建議前者,但編碼在這裏,以配合您現有的代碼...

- (UIImage *)imageForIndexPath:(NSIndexPath *)indexPath { 

    NSDictionary *dic = [self.issues objectAtIndex:indexPath.row]; 
    UIImage *image = [dic valueForKey:@"cached_image"]; 

    if (!image) { 
     image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/issues/%@/cover.png", documentsDirectory, [dic objectForKey:@"Directory Name"]]]; 
     NSMutableDictionary *updatedDictionary = [NSMutableDictionary dictionaryWithDictionary:dic]; 
     [updatedDictionary setValue:image forKey:@"cached_image"]; 
     NSMutableArray *updatedIssues = [NSMutableArray arrayWithArray:self.issues]; 
     [updatedIssues replaceObjectAtIndex:indexPath.row withObject:[NSDictionary dictionaryWithDictionary:updatedDictionary]]; 
     self.issues = [NSArray arrayWithArray:updatedIssues]; 
    } 
    return image; 
} 

這將是通過列表中的第一滾動波濤洶涌,然後平滑其後。如果你想有沒有第一時間印章和承擔一點點延遲出現的視圖之前,您可以提前走你的模型,並迫使負載:

for (int i=0; i<self.issues.count; i++) { 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0]; 
    (void)[self imageForIndexPath:indexPath]; 
} 

最後一兩件事 - 另一種解決方案是一個可變數組來匹配你的問題數組。這可能會很好,但通常情況下,我最終很高興我的模型包含了一些緩存計算。如果您發現自己在其他地方使用該問題陣列,則會很高興您擁有所有照片。

+0

謝謝,我會盡快嘗試!除了複製和粘貼代碼之外,還有什麼我必須做的嗎?對不起,我沒有很多UITableViews的經驗:) – 2012-03-23 14:35:45

+0

不是我能想到的。它應該作爲第一步。下一步是讓你的模型變得可變,這將簡化和加速圖像緩存方法的工作。 – danh 2012-03-23 14:39:02

+0

@danh self.issues = [NSArray arrayWithArray:self.issues];必須是self.issues = [NSArray arrayWithArray:updatedIssues];對? – OzBoz 2013-02-22 12:03:41