【问题标题】:UIImage memory leak issue in ARCARC中的UIImage内存泄漏问题
【发布时间】:2014-06-15 07:29:10
【问题描述】:

我们使用下面的代码在图像视图中设置图像,

@autoreleasepool {
    UIImageView *titleView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ImgTitle" ofType:@"png"]]];
    }

Instruments 在上面两行显示内存泄漏。我们检查了 Xcode 5.0.1 到 iOS7。

请任何人建议解决该问题。

【问题讨论】:

  • 我能知道将代码写入@autoreleasepool 的原因吗??
  • 我们使用了 UIImageView *titleView = [[UIImageView alloc]initWithImage: [UIImage imageWithContentsOfFile: [[NSBundle mainBundle]pathForResource: @"ImgTitle" ofType:@"png"]]];单独的代码。为了减少内存泄漏,我们将代码放在自动释放池中,现在内存泄漏百分比会降低,但仍然会发生泄漏。
  • 你用过这个吗,[游泳池排水管];
  • @Ramdy 和 \@autoreleasepool,他不需要排空池。
  • 在 ARC 下,此代码不应泄漏任何内存,请粘贴您的仪器快照。

标签: ios memory-leaks uiimage automatic-ref-counting instruments


【解决方案1】:

尝试改变

@autoreleasepool
{
UIImageView *titleView = [[UIImageView alloc]initWithImage:
                         [UIImage imageWithContentsOfFile:
                         [[NSBundle mainBundle]pathForResource:
                         @"ImgTitle" ofType:@"png"]]];
}

UIImageView *titleView = [[UIImageView alloc]initWithImage:
                             [UIImage imageWithContentsOfFile:
                             [[NSBundle mainBundle]pathForResource:
                             @"ImgTitle" ofType:@"png"]]];

使用 titleView 后,您可以将其设置为 nil 来处理内存,如下所示。

如果项目启用了 ARC,请使用

titleView=nil;

如果没有,

titleView release];
titleView=nil;`

这会解决你的问题。

【讨论】:

    【解决方案2】:

    对我来说,你的代码正在显示

    Dead Store : Value stored to 'titleView' during its initialization is never read

    这也是有道理的。 没有内存泄漏警告

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-03
      • 2012-09-14
      • 2021-03-31
      • 1970-01-01
      • 2012-10-05
      相关资源
      最近更新 更多