【问题标题】:ios tableview images not freeing memoryios tableview图像不释放内存
【发布时间】:2012-04-02 01:53:14
【问题描述】:

简单场景:表格视图控制器,有 5000 行。 我有 5000 个图像名称,从 0.png 到 4999.png。

这是我的 cellForRowAtIndexPath: 方法:

静态 NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString *string = [NSString stringWithFormat:@"%d",indexPath.row];
UIImage *img = [UIImage imageNamed:string];

if (img) {
    cell.imageView.image  = img;
} else {
    cell.imageView.image = [UIImage imageNamed:@"NoPhotoDefault"];
}   
return cell;

我已经分析了仪器的行为,它表明实际内存使用量在滚动时快速上升,并且从未下降。

它从大约 8 MB 开始,到达最后一行时增加到 200 MB。

我在这里错过了什么?

谢谢

附: Proj 基础 sdk 为 5.0,使用 ARC。

稍后编辑,在后台发送应用程序时,内存被释放。

【问题讨论】:

标签: ios image uitableview memory-management automatic-ref-counting


【解决方案1】:

UIImage 在使用imageNamed: 方法时缓存图像。它确实可以避免在多次使用时重新加载相同的图像。

此方法在系统缓存中查找具有指定名称的图像对象,如果存在则返回该对象。如果缓存中没有匹配的图像对象,则此方法从指定文件加载图像数据,缓存它,然后返回结果对象。

如果您想避免缓存图像,请改用imageWithContentsOfFile:

此方法不缓存图像对象。

【讨论】:

  • 是的,你的权利。我不知道 imagedNamed: 正在缓存图像。
猜你喜欢
  • 2015-06-21
  • 1970-01-01
  • 2010-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-22
  • 1970-01-01
相关资源
最近更新 更多