我看到了一些类似的问题,但没有一个能够帮助我,所以这就是为什么我用我的代码发布这个问题,因为我无法解决问题所在。
UITableView 具有从 Web 服务获取的图像,但滚动似乎很慢。更准确地说,它似乎在滚动时滞后。
这是 cellForRowAtIndexPath 方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ItemsCell";
ItemsViewCell *cell = (ItemsViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
RSSItem *item = [[channel items] objectAtIndex:[indexPath row]];
cell.titleLabel.text = [item title];
cell.creatorLabel.text = [item creator];
cell.pubTimeLabel.text = [item pubTime];
if ([item thumbnail] != nil) {
cell.thumbContainer.image = [item thumbnail];
}
else
cell.thumbContainer.image = [UIImage imageNamed:@"defaultcellImage.png"];
cell.thumbContainer.layer.shadowColor = [[UIColor blackColor] CGColor];
cell.thumbContainer.layer.shadowOffset = CGSizeMake(-3.0, 3.0);
cell.thumbContainer.layer.shadowOpacity = 0.5f;
cell.thumbContainer.layer.shadowRadius = 3.0f;
if ([[TheFeedStore sharedStore] hasItemBeenRead:item]) {
cell.titleLabel.textColor = [UIColor grayColor];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
else
{
cell.titleLabel.textColor = [UIColor blackColor];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
UIImage *background = [self cellBackgroundForRowAtIndexPath:indexPath];
UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:background];
cellBackgroundView.image = background;
cell.backgroundView = cellBackgroundView;
return cell;
}
这是我的 viewDidLoad 方法:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(dataSaved:)
name:@"DataSaved" object:nil];
}
- (void) dataSaved:(NSNotification *)notification{
[self.tableView reloadData];
}
在一个单独的文件中,我正在下载如下图像:
- (void) downloadThumbnails:(NSURL *)finalUrl
{
dispatch_group_async(((RSSChannel *)self.parentParserDelegate).imageDownloadGroup, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSMutableData *tempData = [NSData dataWithContentsOfURL:finalUrl];
[tempData writeToURL:[self cachedFileURLFromFileName:self.thumbFile] atomically:YES];
dispatch_async(dispatch_get_main_queue(), ^{
thumbnail = [UIImage imageWithData:tempData];
[[NSNotificationCenter defaultCenter] postNotificationName:@"DataSaved" object:nil];
});
});
}
当我在模拟器上测试项目时,它看起来很好并且不会滞后,而如果我在设备上尝试它,那么我可以看到这种缓慢的滚动行为。
一切似乎都很好。谁能帮我指出问题出在哪里。请不要建议使用 SDWebImage 或任何其他图像库,如果您只想推荐 Apple 的 LazyTableImage 项目,也不要回答。谢谢
更新:经过一番搜索,看起来减小图像的大小可能会有所帮助,但由于某些原因,可用的解决方案对我不起作用。