【问题标题】:renderInContext taxes App MemoryrenderInContext 对应用程序内存征税
【发布时间】:2013-11-07 22:27:36
【问题描述】:

我在 2448 X 2448 像素的图像上运行此代码。 fullScaleView 也是 2448 X 2448 (fullScreenView Rect:{{0, 0}, {2448, 2448}})。方法完成后 App 内存从 49.7MB 跳到 240MB 下降到 172MB。它保持在 172MB。在renderInContext 之后,该应用程序似乎不应该仍然以如此高的内存占用运行。我应该在哪里以及如何强制释放? (iOS 7 XCode 5 ARC)。

UIGraphicsBeginImageContextWithOptions(fullScaleView.bounds.size, fullScaleView.opaque, 1.0);
[fullScaleView.layer renderInContext: UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

【问题讨论】:

  • 您是否在方法之外保留了对viewImage 的引用?
  • 是的,我在 UIGraphicsEndImageContext() 之后直接返回 viewImage
  • 这可能是内存的大量增加。它将存储(我认为)在未压缩的位图中。 2448x2448x32 为 192m。
  • 为什么是 32?应该是 4 点?
  • 我在考虑 32 位图像,但就像我说的那样,我真的不知道这个数学。无论哪种方式,请检查图像是否正在使用该内存。

标签: ios iphone memory uiimageview uigraphicscontext


【解决方案1】:

内存跳跃是因为图像很大 - 如果您确定不再需要它,您应该将使用返回图像的任何地方包装在一个自动释放块中:

例如

@autoreleasepool {
    UIImage *theReturnedImage = yourmethodthatreturnstherenderedimage();
    // do stuff with your image
}

不幸的是,在您使用完图像之前,它会占用空间,所以您必须快速释放它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    • 2013-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多