【问题标题】:UIGraphicsGetImageFromCurrentImageContext leakUIGraphicsGetImageFromCurrentImageContext 泄漏
【发布时间】:2011-11-03 16:54:12
【问题描述】:

我已经编写了以下代码 sn-p 来截屏:

UIGraphicsBeginImageContext(animationView.frame.size);
[[window layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage* screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

但是,UIGraphicsGetImageFromCurrentImageContext 似乎正在泄漏。这是正确的吗?

在 Instruments 中,我无法获得确切的泄漏点。在活动监视器中,我观察到当我切换到执行上述代码的 UI 时,sn-p 内存会增加一些 MB。在此之后,它永远不会减少。 UIGraphicsGetImageFromCurrentImageContext 有内存泄漏吗?我该如何解决?

编辑

仪器分析 Activity Monitor:显示执行这行代码时的内存增加;即使发布截图(UIImage)也不会减少

泄漏和分配,堆快照:不显示任何泄漏或此分配。

【问题讨论】:

标签: ios image cocoa-touch memory memory-leaks


【解决方案1】:

CGContextRef 上下文 = UIGraphicsGetCurrentContext();

/* 你的代码 */

CGContextRelease(上下文);

完成后清除上下文

【讨论】:

    【解决方案2】:

    您刚刚创建了一个 UIImage,其中包含您的动画视图的数据(可能是一些 MB)。也许您应该将此功能包装在自动释放池中。

     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];     
        UIImage* screenshot = UIGraphicsGetImageFromCurrentImageContext();
        //[screenshot retain]; //If you want precise control over when it is released and you will use it later.
        [pool release];
    

    【讨论】:

    • 我没有保留“截图”。那我需要释放它吗?
    • 其实我猜 UIGraphicsGetImageFromCurrentImageContext 是自动发布的。尝试将其包装在自动释放池中。
    • 我试过这个。但是,没有用。当我使用调用 [[UIImage alloc] initWithContentsOfFile:] 创建带有一些虚拟图像的屏幕截图时,活动监视器中的实际内存在释放后减少了。
    猜你喜欢
    • 2013-11-26
    • 2011-07-04
    • 2011-11-18
    • 2011-11-02
    • 1970-01-01
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多