即使发布了CIImageRefs,CIContext内存泄漏

wil*_*urn 7 xcode memory-leaks objective-c ios

我正在使用此处找到的解决方案来使用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);
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

Gaz*_*ini 2

编辑:

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

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


我的回答是从这个问题衍生出来的。

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

CGImageRelease(cgimage);
Run Code Online (Sandbox Code Playgroud)

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

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

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

  • 代码中的上下文是“CIContext”,您无法使用 CGContextRelease 释放它...没有 CIContextRelease,afaik,所以这仍然需要帮助! (3认同)