Ios 具有异步图像加载功能的表视图
我有一个带有背景视图和一些文本的表视图。我下载的背景从一个网址在下面的代码它的硬编码,但我会有不同的网址为每个细胞。为了提高响应速度,我使用了一个图像缓存来保存下载的图像,并异步加载这些图像。我正在为图像使用淡入动画。除了一些奇怪的行为,一切正常。第一次,图像加载良好。如果我滚动,图像的大小将调整为每个单元格中的其他内容。此外,如果我停止滚动,我可以看到图像在每个单元格中随机重新加载。有时,图像会消失,几秒钟后重新出现 我在iOS方面还不够好,无法解决这个问题,请帮助我 以下是我的cell数据源方法:Ios 具有异步图像加载功能的表视图,ios,Ios,我有一个带有背景视图和一些文本的表视图。我下载的背景从一个网址在下面的代码它的硬编码,但我会有不同的网址为每个细胞。为了提高响应速度,我使用了一个图像缓存来保存下载的图像,并异步加载这些图像。我正在为图像使用淡入动画。除了一些奇怪的行为,一切正常。第一次,图像加载良好。如果我滚动,图像的大小将调整为每个单元格中的其他内容。此外,如果我停止滚动,我可以看到图像在每个单元格中随机重新加载。有时,图像会消失,几秒钟后重新出现 我在iOS方面还不够好,无法解决这个问题,请帮助我 以下是我的cell数据源
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row==0){
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"identifier"];
[cell.contentView addSubview:self.headerFrame];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
static NSString *cellIdentifier = @"HistoryCell";
// Similar to UITableViewCell, but
CustomSaloonCell *cell = (CustomSaloonCell *)[theTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[CustomSaloonCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSString *identifier = [NSString stringWithFormat:@"Cell%ld" ,
(long)indexPath.row];
if([self.cachedImages objectForKey:identifier] != nil){
cell.backgroundView = [[UIImageView alloc]initWithImage:[cachedImages valueForKey:identifier]];
}
else {
char const * s = [identifier UTF8String];
dispatch_queue_t queue = dispatch_queue_create(s, 0);
dispatch_async(queue, ^{
NSURL *url = [NSURL URLWithString:@"http://upload.wikimedia.org/wikipedia/en/thumb/7/73/Shrekcharacter.jpg/220px-Shrekcharacter.jpg"];
UIImage *img = nil;
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
img = [[UIImage alloc] initWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
if ([theTableView indexPathForCell:cell].row == indexPath.row) {
[cachedImages setValue:img forKey:identifier];
cell.backgroundView = [[UIImageView alloc]initWithImage:[cachedImages valueForKey:identifier]];
cell.backgroundView.alpha = 0.0;
[UIView animateWithDuration:5.0 animations:^{
cell.backgroundView.alpha = 1.0;
}];
}
});
});
}
/********/
cell.stylistName.text = [salonDetailsArray objectAtIndex:indexPath.row][@"salonName"];
cell.salonAddress.text = [salonDetailsArray objectAtIndex:indexPath.row][@"salonAddress"];
cell.delegateListener = self;
cell.claimButton.frame =CGRectMake(494/2, 216/2, 120/2, 46.0/2.0);
[cell.claimButton addTarget:self action:@selector(claimClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.claimButton setTitle:@"claim" forState:UIControlStateNormal];
if([loginStatus isEqualToString:@"YES"]){
[cell.claimButton setTitleColor:[UIColor colorWithRed:106.0/255.0 green:45.0/255.0 blue:118.0/255.0 alpha:1.0] forState:UIControlStateNormal];
}else {
[cell.claimButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
}
cell.claimButton.tag = indexPath.row;
cell.overlay.hidden = YES;
for (NSIndexPath *selectedIndex in self.buttonStateArray){
if([selectedIndex isEqual:indexPath]){
[cell.claimButton setTitle:@"claimed" forState:UIControlStateNormal];
cell.claimButton.frame = CGRectMake(464/2-(30/2), 216/2, 180/2, 46.0/2.0);
cell.overlay.hidden = NO;
[cell.claimButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
}
return cell;
}
使用您自己的代码下载和缓存图像是很好的,但不推荐这样做,因为有许多高度优化的类可以很巧妙地完成这项工作。 我更喜欢为业务逻辑编写自己的代码,使用优化的库实现性能是很有帮助的 SDWebImage可以满足您的需求,支持缓存、异步图像下载,完成块也可以用于制作动画。也可以尝试。它会自动将UIImage/NSData缓存在磁盘或内存中 这是GitHub的解释图: 还有一些代码示例。
祝你好运。这是第二大问题。当你更努力地搜索时,你会找到有价值的答案;主要的问题是,您正在完成处理程序中捕获单元格-在处理程序执行期间可能会重用该单元格,因此,请捕获indexPath并再次获取单元格!。您还应该使用适当的方法来加载远程资源。