CIFilters导致应用程序崩溃

时间:2013-12-13 09:09:40

标签: ios objective-c memory-management core-image

我需要有关如何在内存中有效使用CIFilter的建议。基本上,我将两个过滤器链接在一起。棘手的部分是我必须在调用过滤器之前先将UIImage转换为CIImage。代码发布在下面:

CIImage *foreground = [CIImage imageWithCGImage:first.CGImage options:nil];
CIImage *background = [CIImage imageWithCGImage:second.CGImage options:nil];

CIFilter *softLightBlendFilter = [CIFilter filterWithName:@"CISoftLightBlendMode"];
[softLightBlendFilter setDefaults];
[softLightBlendFilter setValue:foreground forKey:kCIInputImageKey];
[softLightBlendFilter setValue:background forKey:kCIInputBackgroundImageKey];

CIImage *result = [softLightBlendFilter outputImage];

foreground = nil;
background = nil;
softLightBlendFilter = nil;

CIFilter *gammaAdjustFilter = [CIFilter filterWithName:@"CIGammaAdjust"];
[gammaAdjustFilter setDefaults];
[gammaAdjustFilter setValue:result forKey:kCIInputImageKey];
[gammaAdjustFilter setValue:[NSNumber numberWithFloat:value] forKey:@"inputPower"];
result = [gammaAdjustFilter valueForKey:kCIOutputImageKey];

gammaAdjustFilter = nil;

CIContext *context = [CIContext contextWithOptions:nil];
CGRect extent = [result extent];
CGImageRef cgImage = [context createCGImage:result fromRect:extent];

UIImage *image = [UIImage imageWithCGImage:cgImage scale:1.0 orientation:first.imageOrientation];

CFRelease(cgImage);
result = nil;

return image;

此代码可轻松拍摄高达200MB的内存,这将导致应用程序崩溃。

那么我做错了什么?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

调用image.CGImage将图像加载到内存中。

您可以尝试使用

CIImage *ciImage = [CIImage imageWithContentsOfURL:imageUrl options:@{kCIImageColorSpace:[NSNull null]}];

CIImage * ciImage = [CIImage imageWithCGImage:first.CGImage options:nil];

而不是

CIImage *ciImage = [CIImage imageWithCGImage:uiImage.CGImage options:nil];

另一种选择是平铺图像,在瓷砖上应用滤镜,然后再将它们拼接在一起..