【问题标题】:CIContext Memory Leak even with released CIImageRefsCIContext 内存泄漏,即使已发布 CIImageRefs
【发布时间】:2014-06-03 07:25:47
【问题描述】:

我正在使用here 找到的解决方案来使用 CIGaussianBlur 模糊图像。但是,我遇到了无法解决的内存泄漏问题。我最初使用不使用 CIContext 作为属性,但认为这可能是无济于事的问题。我也使用了输出图像中的 CGRect,但更改了它以尝试关闭泄漏,再次没有工作。

我相信我正在释放我需要的所有内容(ARC 已打开),那么可能导致内存泄漏的原因是什么?

    CIFilter *gaussianBlurFilter = [CIFilter filterWithName: @"CIGaussianBlur"];
    CGImageRef cgimage = [image CGImage];
    [gaussianBlurFilter setValue:[CIImage imageWithCGImage:cgimage] forKey:kCIInputImageKey];
    [gaussianBlurFilter setValue:@10 forKey:kCIInputRadiusKey];
    CIImage *outputImage = [gaussianBlurFilter outputImage];
    if (imageContext == nil) {
        imageContext = [CIContext contextWithOptions:nil];
    }
    CGImageRef cgimg     = [imageContext createCGImage:outputImage fromRect:CGRectMake(0.0, 0.0, 25.0, 25.0)];
    UIImage *blurredImage       = [UIImage imageWithCGImage:cgimg];

    pictureIncognito.image = blurredImage;
    pictureIncognito.layer.cornerRadius = pictureIncognito.frame.size.width / 2.0;
    pictureIncognito.layer.masksToBounds = YES;
    pictureIncognito.layer.borderWidth = 1.0;
    pictureIncognito.layer.borderColor = [[UIColor whiteColor] CGColor];

    CGImageRelease(cgimage);
    CGImageRelease(cgimg);

【问题讨论】:

  • 当你在 Instruments.app 下运行它时,你应该很快就能找到泄漏的原因。
  • 对不起,一定是错过了——太小了,没有上下文! ;-) 我的错。
  • 嗨,你有答案吗?我也面临同样的问题。可以给我答案吗?

标签: ios objective-c xcode memory-leaks


【解决方案1】:

编辑:

https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGContext/Reference/reference.html#//apple_ref/c/func/CGContextRelease

我今天遇到了这个。事实上,你确实需要使用这个特殊函数来释放上下文。


我的回答来源于this question.

作为观察,我认为您不需要这一行:

CGImageRelease(cgimage);

您实际上并不拥有该对象(您用来设置它的方法中没有“get”、“alloc”、“create”或“new”)。

我对 CGImage 没有太多经验,但我想上下文仍然存在于 Objective-C 运行时的某个地方,并且上下文以某种方式保留了图像本身。因此,将上下文(以及其他所有内容)设置为 nil 可能会解决问题。

当然,如果这样可行,则意味着您将为每个模糊的图像创建一个新的上下文,并且可能会影响您以后修改图像的能力......但它可能会解决内存泄漏!

【讨论】:

  • 第一个CGImageRelease 绝对正确,谢谢。这并没有完全解决泄漏,但我认为你让我找到了问题的正确轨道。我和你一样,倾向于远离 CGImage,实际上选择使用一些 WWDC2013 代码:UIImage+ImageEffects
  • 代码中的上下文是“CIContext”,你不能用CGContextRelease释放它...没有CIContextRelease,afaik,所以这仍然需要帮助!
猜你喜欢
  • 2016-01-19
  • 1970-01-01
  • 1970-01-01
  • 2013-04-10
  • 1970-01-01
  • 1970-01-01
  • 2019-11-22
  • 2012-07-18
  • 2014-08-18
相关资源
最近更新 更多