您好,我正在开发一个应用程序,我将从网络服务中获取图像并将图像加载到表格 View 中。我异步加载图像。问题是我的应用程序在滚动 TableView 时崩溃,并在日志中显示内存收到警告。而且相同的图像在许多行中重复。而且加载需要更多时间。我使用了下面的代码。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 静态 NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
/* UILabel * cellLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 50, cell.frame.size.width-20, 45)];
cellLabel.textColor=[UIColor blackColor];
cellLabel.backgroundColor=[UIColor clearColor];
cellLabel.tag=2;
[cell.contentView addSubview:cellLabel];*/
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.backgroundView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"list_iPhone.png"]];
UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(10,18, 48, 48)];
imv.tag=4;
imv.image=[UIImage imageNamed:@"ImagePlaceholder.png"];
[cell.contentView addSubview:imv];
UIImageView *arrwimv = [[UIImageView alloc]initWithFrame:CGRectMake(260,35, 14, 17)];
arrwimv.image=[UIImage imageNamed:@"arrw_iPhone.png"];
[cell.contentView addSubview:arrwimv];
UILabel *descriptionLbl=[[UILabel alloc] initWithFrame:CGRectMake(100, 27, 450, 45)];
descriptionLbl.font=[UIFont CITY311_TitleFontWithSize:18];
descriptionLbl.tag=1;
descriptionLbl.textAlignment=UITextAlignmentLeft;
descriptionLbl.textColor=[UIColor blackColor];
descriptionLbl.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:descriptionLbl];
UILabel *descriptionLbl2=[[UILabel alloc] initWithFrame:CGRectMake(100, 5, 450, 45)];
descriptionLbl2.font=[UIFont CITY311_TitleFontWithSize:18];
descriptionLbl2.tag=2;
descriptionLbl2.textAlignment=UITextAlignmentLeft;
descriptionLbl2.textColor=[UIColor blackColor];
descriptionLbl2.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:descriptionLbl2];
}
UIImageView *imv2=(UIImageView *)[cell.contentView viewWithTag:4];
dispatch_async(mainQueue, ^(void) {
if(![[[issueArray objectAtIndex:indexPath.row]objectForKey:@"PhotoUrl"] isEqualToString:@""])
{
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[[issueArray objectAtIndex:indexPath.row]objectForKey:@"PhotoUrl"]]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
imv2.image = image;
}
});
UILabel *lbl=(UILabel *)[cell.contentView viewWithTag:1];
lbl.text=[[issueArray objectAtIndex:indexPath.row] objectForKey:@"issueSubmittedDate"];
UILabel *lbl2=(UILabel *)[cell.contentView viewWithTag:2];
lbl2.text=[[issueArray objectAtIndex:indexPath.row] objectForKey:@"IssueName"];
return cell;
}
View 中已加载
mainQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
在.h文件中
dispatch_queue_t mainQueue;
请帮助正确加载图像,而不会出现任何内存警告(崩溃)。提前致谢。
最佳答案
您正在重用 tableviewcell。当单元格移出屏幕时,它将被放在一边,以便您可以重复使用该对象。当您执行 dequeueReusableCellWithIdentifier 时,您可以获得一个已包含图像的“旧”单元格。如果您不清除该单元格中的数据,您将看到旧数据(图像),直到下载新图像。
在 if(cell==nil) 中,您应该只创建单元格并设置每行都相同的属性。设置和清除该if下面的数据。
发生崩溃的原因可能是在初始回调准备就绪之前,单元格可以移出 View 并重新用于其他行。尝试将标识符设置为唯一值。就像是: NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d", indexPath.Row]; 仅当行数较少时才保留这样的代码。否则你可能会遇到内存问题。
您可以尝试使用类似 https://github.com/rs/SDWebImage 的内容,而不是尝试自己修复它。
关于ios - 如何在uitableview中异步加载图像而不出现内存警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14134146/