2012-03-23 67 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