神秘的CoreImage内存泄漏问题在使用ARC时出现

4
我遇到了一些巨大的内存泄漏问题,使用“Leaks”工具检测不出来。当我弹出一个模态视图控制器并对4或5个不同的图片应用2个CoreImage过滤器时,使用Instruments可以看到内存上升约40-50 MB,但即使在我关闭模态视图控制器后,也无法恢复该内存,并且当我重复这个过程2-3次后,应用程序将崩溃。如果您能提供任何建议,我会非常感激,因为这让我疯狂。以下是相关方法:
UIView *finalView = [[UIView alloc] initWithFrame:CGRectMake(1024, 0, 1792, 1345)];
UIImageView *templateImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1792, 1345)];
templateImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png",[theme objectForKey:@"template_background"]]];

//CI background Setup
NSString *filePath5 = [[NSBundle mainBundle] pathForResource:[theme objectForKey:@"template_background"] ofType:@"png"];
NSURL *fileNameAndPath5 = [NSURL fileURLWithPath:filePath5];

    @autoreleasepool {

        finalBackBeginImage = [CIImage imageWithContentsOfURL:fileNameAndPath5];
        finalBackImage = [CIFilter filterWithName:@"CIHueAdjust" keysAndValues:@"inputAngle", [NSNumber numberWithFloat:[[boothPrefs objectForKey:@"templateBackground_hue"] floatValue]*6.28], @"inputImage", finalBackBeginImage, nil].outputImage;
        finalBackImage = [CIFilter filterWithName:@"CIColorControls" keysAndValues:@"inputSaturation", [NSNumber numberWithFloat:([[boothPrefs objectForKey:@"templateBackground_saturation"] floatValue] * 5)], @"inputImage", finalBackImage, nil].outputImage;

        finalBackContent = [CIContext contextWithOptions:nil];
        CGImageRef cgimgFinalBack = 
        [finalBackContent createCGImage:finalBackImage fromRect:[finalBackImage extent]];
        UIImage *newFinalBackImg = [UIImage imageWithCGImage:cgimgFinalBack];
        [templateImageView setImage:newFinalBackImg];
        CGImageRelease(cgimgFinalBack);

    }

[finalView addSubview:templateImageView];

参考一下,被修改的几张图片是1024 x 768的png格式,还有一些可能更大,也许是1700 x 1300。我理解为什么会需要大量内存来处理这些滤镜,但我不明白为什么它们没有被释放。 - Justin Gaynor
下次调用此函数之前,您是否已经删除了templateImageView? - Sunil Pandey
不,每个添加到finalView的图像都有自己的UIImageView。templateImageView只是其中的第一个。 - Justin Gaynor
你不是唯一一个被这个问题折磨得快要发疯的人!这真的很糟糕,我将其缩小到使用CIFilters... https://dev59.com/mnDYa4cB1Zd3GeqPAG7L - Alex Stone
1个回答

2
我已经从使用imageNamed切换到使用下面的代码中的imageWithData。在测试了5分钟后(抱歉,我现在已经花了接近12个小时来解决这个问题),我发现相同操作的实际内存使用量低了多达50%(115mb与最高可达230mb),并且神秘的“Push +80mb,pop-30mb”实际内存问题似乎已得到解决。
尽管如此,我还是抱着好奇的心态。
//use images like this as base images for CIFilters
    NSData* imageData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:self.frameName ofType:nil]];
        ;
        UIImage* imageForFilter =[UIImage imageWithData: imageData];

imageNamed:会缓存生成的图像,因此它主要用于多次使用的图像(例如工具栏图标等)。 - omz
这刚刚为我们节省了70兆字节。谢谢。 - Michael Marcin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接