我在我的应用程序中使用Core Image滤镜,在iPhone 5设备上运行iOS 7时一切正常,但当我在只有512MB总内存的iPhone 4s上测试时,应用程序会崩溃。
情况如下:我有两张从相机拍摄的图片,每张分辨率为2448x3264。在我的iPhone 5上,整个过程的峰值占用了150MB,根据工具显示。
然而,当我尝试在iPhone 4s上运行相同的代码时,即使整个内存使用很低(约为8 MB),仪器也一直给出内存不足警告。以下是屏幕截图。
以下是代码,基本上,我从我的应用程序文档文件夹中加载了两个图像,并连续应用了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];
有没有更节省内存的处理方式,或者我在这里做错了什么?非常感谢!