使用 ARC 的神秘 CoreImage 内存泄漏
我遇到了一些大量的内存泄漏,但使用“泄漏”工具无法显示出来。我弹出一个模态视图控制器,并将 2 个 CoreImage 滤镜应用于 4 或 5 个不同的图像。使用 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];
I'm experiencing some massive memory leaks that don't show up using the "leaks" instrument. I pop up a Modal View Controller and apply 2 CoreImage filters to 4 or 5 different images. Using Instruments I can see the memory jump up about 40-50 MB as these images are created, but even after I dismiss the Modal View Controller, I never get that memory back, and the application will crash after repeating this process 2 or 3 times. I'm happy for any advice you can provide because this is driving me absolutely crazy. Below is the method in question:
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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已使用下面的代码从使用 imageNamed 切换到使用 imageWithData。在 5 分钟的测试中(抱歉,现在在这个问题上花费了近 12 个小时),我发现同一操作的实际内存使用量降低了 50%(115mb 与 230mb),并且神秘的“推+80mb,弹出-30mb”实际内存问题似乎已经解决。
不过我还是祈祷吧。
I've switched from using imageNamed to using imageWithData using the code below. In 5 minutes of testing (sorry, spent close to 12 hours on this issue now), I see that my real memory usage for the same operation is up to 50% lower (115mb versus up to 230 mb) and the mysterious "Push +80mb, pop -30mb" real memory issue appears to be solved.
I'm keeping my fingers crossed though.