【问题标题】:UIGraphicsContext memory leakUIGraphicsContext 内存泄漏
【发布时间】:2013-10-01 19:39:46
【问题描述】:

嗨,在我的应用程序中,我有一个函数,它获取当前视图的图像并将其转换为模糊图像,然后将其添加到 current.view。尽管我使用 [remove from superview] 删除了视图,但内存仍然很高。我正在使用核心图形并将所有 UI 图像设置为零。

我确实收到了内存泄漏警告

-(void)burImage
{
    //Get a screen capture from the current view.
    UIGraphicsBeginImageContext(CGSizeMake(320, 450));
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    //Blur the image
    CIImage *blurImg = [CIImage imageWithCGImage:viewImg.CGImage];

    CGAffineTransform transform = CGAffineTransformIdentity;
    CIFilter *clampFilter = [CIFilter filterWithName:@"CIAffineClamp"];
    [clampFilter setValue:blurImg forKey:@"inputImage"];
    [clampFilter setValue:[NSValue valueWithBytes:&transform objCType:@encode(CGAffineTransform)] forKey:@"inputTransform"];

    CIFilter *gaussianBlurFilter = [CIFilter filterWithName: @"CIGaussianBlur"];
    [gaussianBlurFilter setValue:clampFilter.outputImage forKey: @"inputImage"];
    [gaussianBlurFilter setValue:[NSNumber numberWithFloat:22.0f] forKey:@"inputRadius"];

    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef cgImg = [context createCGImage:gaussianBlurFilter.outputImage fromRect:[blurImg extent]];
    UIImage *outputImg = [UIImage imageWithCGImage:cgImg];

    //Add UIImageView to current view.
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 450)];
    [imgView setTag:1109];

    imgView.image = outputImg;
    [imgView setTag:1108];

    gaussianBlurFilter = nil;
    outputImg = nil;
    blurImg = nil;

    viewImg = nil;
    [self.view addSubview:imgView];
    UIGraphicsEndImageContext();
}

【问题讨论】:

  • 你的项目使用 ARC 吗?
  • 顺便说一句,我认为您的方法末尾有一个额外的UIGraphicsEndImageContext()。我不知道那个额外的调用是否有有害影响,但你应该删除它。
  • 另外,我不明白您两次调用setTag 的意图是(因为第二次仅覆盖第一次的值)。而且您没有回答 subj singh 的重要问题:这很重要,因为如果不使用 ARC,您也可能会泄露您的 UIImageView
  • setTag 两次是错误的。我用它来轻松地从根视图中删除视图。

标签: iphone ios memory uigraphicscontext


【解决方案1】:

静态分析器(Xcode“产品”菜单上的“分析”)会在方法结束时通知您缺少所需的CGImageRelease(cgImg)。如果您有一个从名称中带有“Create”或“Copy”的方法/函数返回的 Core 基础对象,则您有责任释放它。

顺便说一句,如果您点击 图标(一次在空白处,然后再次点击出现在错误消息中的版本),它将显示更多信息:

这有助于追溯问题的起源,在本例中是调用createCGImage。如果您查看documentationcreateCGImage,它会确认此诊断,报告:

返回值

石英 2D 图像。当您不再需要返回的图像时,您有责任释放它。

有关发布 Core Foundation 对象的一般法律顾问,请参阅 Core Foundation 内存管理编程指南中的Create Rule

【讨论】:

  • 谢谢 Rob 我已经添加了 CGImageRelease(cgImg) 但每次调用它时它仍然会占用内存。我会检查创建规则谢谢
  • 是的,我正在使用 ARC。内存泄漏现在从 Analyzer 中消失了。我已经删除了第二个 EndImageContext。我已经部分运行了代码,一旦它到达 CGImageRef cgImg = [context createCGImage:gaussianBlurFilter.outputImage fromRect:[blurImg extent]];它跳跃了 15mb,但添加后我使用 CGImageRelease(cgImg) 释放
  • 最烦人的是我已经单步执行了代码,一旦我分配了一个图像引用,内存就会增加,即使我尝试立即释放它仍然保持不变。
  • CIContext *context = [CIContext contextWithOptions:nil]; CGImageRef cgImg = [context createCGImage:gaussianBlurFilter.outputImage fromRect:[blurImg extent]]; CGImageRelease(cgImg); // 这只是为了检查它是否会释放内存
  • @Mike 我们已经用我的回答解决了您代码中的问题。我们现在正试图追查一些次要的泄漏源,所以我们应该continue this discussion in chat
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-08
  • 2013-01-20
  • 2011-10-31
  • 2019-08-10
  • 2013-06-24
  • 2011-03-22
  • 2015-04-20
相关资源
最近更新 更多