【发布时间】:2014-01-12 18:11:42
【问题描述】:
我在我的应用程序中使用 Core Image 过滤器,在运行 iOS 7 的 iPhone 5 设备上一切正常,但是当我在总内存仅为 512MB 的 iPhone 4s 上测试它时,应用程序崩溃了。
情况是这样,我从相机拍摄了 2 张图像,每张的分辨率为 2448x3264。在我的 iPhone 5 中,根据仪器,整个过程在峰值时占用 150MB。
但是,当我尝试在 iPhone 4s 上运行相同的代码时,即使整个内存使用量非常低(大约 8 MB),仪器也会一直给我内存不足的警告。这是下面的屏幕截图。
这是代码,基本上,我从我的应用程序的文档文件夹中加载了两张图片,并连续应用了 2 个过滤器:
CIImage *foreground = [[CIImage alloc] initWithContentsOfURL:foregroundURL];
CIImage *background = [[CIImage alloc] initWithContentsOfURL:backgroundURL];
CIFilter *softLightBlendFilter = [CIFilter filterWithName:@"CISoftLightBlendMode"];
[softLightBlendFilter setDefaults];
[softLightBlendFilter setValue:foreground forKey:kCIInputImageKey];
[softLightBlendFilter setValue:background forKey:kCIInputBackgroundImageKey];
foreground = [softLightBlendFilter outputImage];
background = nil;
softLightBlendFilter = nil;
CIFilter *gammaAdjustFilter = [CIFilter filterWithName:@"CIGammaAdjust"];
[gammaAdjustFilter setDefaults];
[gammaAdjustFilter setValue:foreground forKey:kCIInputImageKey];
[gammaAdjustFilter setValue:[NSNumber numberWithFloat:value] forKey:@"inputPower"];
foreground = [gammaAdjustFilter valueForKey:kCIOutputImageKey];
gammaAdjustFilter = nil;
CIContext *context = [CIContext contextWithOptions:nil];
CGRect extent = [foreground extent];
CGImageRef cgImage = [context createCGImage:foreground fromRect:extent];
UIImage *image = [UIImage imageWithCGImage:cgImage scale:1.0 orientation:imgOrientation];
CFRelease(cgImage);
foreground = nil;
return image;
应用程序在此行崩溃:CGImageRef cgImage = [context createCGImage:foreground fromRect:extent];
有没有更节省内存的方法来处理这种情况,或者我在这里做错了什么?
非常感谢!
【问题讨论】:
-
将一些相关代码放入@autoreleasepool {相关代码}可能会有所帮助?
-
我使用的是 ARC,所以我猜一切都在自动释放池中?
-
自己试试看。
-
=> 查找 NSAutoReleasePool 的 Xcode 文档
-
@DigiMonk,我在自动释放池中扭曲了上面的全部代码,但应用程序仍然崩溃,你建议的
relevant code是什么?
标签: ios iphone objective-c memory core-image