我正在将图片加载到与单元格文本对应的表格视图中,因此每个图像都不相同。随着用户向下滚动,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;
}
结帐UIImageLoader。它正是你想要的,只有两个文件要添加到Xcode。这也是非常容易理解的,回购中有两个很棒的样本。 https://github.com/gngrwzrd/UIImageLoader – gngrwzrd 2015-12-25 20:36:24