我们的应用程序使用 GPUImage 框架来处理图像。我们正在使用框架内的几乎所有过滤器和混合模式。我们按照我们应该的方式分配和释放内存。但是,尽管我们的“实时”内存保持在 2.5MB 左右,有时会飙升至 10MB 但又回落至 2.5MB,但毫无疑问,该应用程序在保存大约 13 张使用覆盖或边框的图像后会崩溃。所以,在深入研究这个问题并被它完全愚蠢的时候,我看到了一些“检查活动监视器以了解你的“真实”内存使用情况”的内容。所以我只是发现“真实”内存一直在攀升,直到大约 250MB 真实和 480MB 虚拟应用程序将崩溃。我正在按我应该的方式释放我的内存。
这是我们用来处理图像的代码。这将获取图像,将其发送到第一个过滤器,分配过滤器,处理图像,释放分配的过滤器,然后将处理后的图像发送到下一个过滤器进行处理,具体取决于启用与否的过滤器。如果我在这里做错了什么,我欢迎所有的批评和建议!我不是专业人士!就我而言,处理 13 张 1536 x 2048 大小的图像不会导致应用程序崩溃,如果您正确处理内存的话。非常感谢你提前拯救了我剩下的头发。
吉姆
-(id)processFilters{
NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init];
self.previewView.image = [UIImage imageWithContentsOfFile:[self.documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"thumb_%@", self.file]]];
if (sephiaFilterEnabled){
sephiaFilter = [[GPUImageSepiaFilter alloc] init];
[(GPUImageSepiaFilter *)sephiaFilter setIntensity:[_filterSephiaSlider value]];
self.previewView.image = [sephiaFilter imageByFilteringImage:previewView.image];
[sephiaFilter release];
}
if (saturationFilterEnabled){
saturationFilter = [[GPUImageSaturationFilter alloc] init];
[(GPUImageSaturationFilter *)saturationFilter setSaturation:[_filterSaturationSlider value]];
self.previewView.image = [saturationFilter imageByFilteringImage:previewView.image];
[saturationFilter release];
}
if (contrastFilterEnabled){
contrastFilter = [[GPUImageContrastFilter alloc] init];
[(GPUImageContrastFilter *)contrastFilter setContrast:[_filterContrastSlider value]];
self.previewView.image = [contrastFilter imageByFilteringImage:previewView.image];
[contrastFilter release];
}
if (brightnessFilterEnabled){
brightnessFilter = [[GPUImageBrightnessFilter alloc] init];
[(GPUImageBrightnessFilter *)brightnessFilter setBrightness:[_filterBrightnessSlider value]];
self.previewView.image = [brightnessFilter imageByFilteringImage:previewView.image];
[brightnessFilter release];
}
if (exposureFilterEnabled){
exposureFilter = [[GPUImageExposureFilter alloc] init];
[(GPUImageExposureFilter *)exposureFilter setExposure:[_filterExposureSlider value]];
self.previewView.image = [exposureFilter imageByFilteringImage:previewView.image];
[exposureFilter release];
}
if (sharpenFilterEnabled){
sharpenFilter = [[GPUImageSharpenFilter alloc] init];
[(GPUImageSharpenFilter *)sharpenFilter setSharpness:[_filterSharpenSlider value]];
self.previewView.image = [sharpenFilter imageByFilteringImage:previewView.image];
[sharpenFilter release];
}
if (vignetteFilterEnabled){
vignetteFilter = [[GPUImageVignetteFilter alloc] init];
[(GPUImageVignetteFilter *)vignetteFilter setVignetteEnd:[_filterVignetteSlider value]];
self.previewView.image = [vignetteFilter imageByFilteringImage:previewView.image];
[vignetteFilter release];
}
if (gBlurFilterEnabled){
if(blurRadialEnabled){
gBlurFilter = [[GPUImageGaussianSelectiveBlurFilter alloc] init];
float yy;
float xx;
yy = blurToPoint.y / 320;
xx = blurToPoint.x / 240;
//NSLog (@"%f %f", xx, yy);
if(blurToPoint.x == 0 && blurToPoint.y == 0){
xx = 0.5;
yy = 0.5;
}
[(GPUImageGaussianSelectiveBlurFilter *)gBlurFilter setExcludeCirclePoint:CGPointMake(xx, yy)];
[(GPUImageGaussianSelectiveBlurFilter *)gBlurFilter setExcludeCircleRadius:[_filterGBlurSlider value]];
[(GPUImageGaussianSelectiveBlurFilter *)gBlurFilter setExcludeBlurSize:0.6];
self.previewView.image = [gBlurFilter imageByFilteringImage:previewView.image];
[gBlurFilter release];
}else if(blurBoxEnabled){
gBlurBoxFilter = [[GPUImageBoxBlurFilter alloc] init];
self.previewView.image = [gBlurBoxFilter imageByFilteringImage:previewView.image];
[gBlurBoxFilter release];
}else if(blurTiltEnabled){
gBlurTiltFilter = [[GPUImageTiltShiftFilter alloc] init];
float yy;
yy = blurToPoint.y / 320;
if(blurToPoint.y == 0){
yy = 0.5;
}
[(GPUImageTiltShiftFilter *)gBlurTiltFilter setTopFocusLevel:yy - [_filterTiltBlurSlider value]];
[(GPUImageTiltShiftFilter *)gBlurTiltFilter setBottomFocusLevel:yy + [_filterTiltBlurSlider value]];
self.previewView.image = [gBlurTiltFilter imageByFilteringImage:previewView.image];
[gBlurTiltFilter release];
}
}
if (cropFilterEnabled){
cropFilter = [[GPUImageCropFilter alloc] init];
float wBuff = [_filterCropSlider value] * cropToPoint.x;
float hBuff = [_filterCropSlider value] * cropToPoint.y;
float xp = cropToPoint.x - wBuff;
float yp = cropToPoint.y - hBuff;
float t1 = xp * [_filterCropSlider value];
float t2 = yp * [_filterCropSlider value];
xp = xp + (t1 * 4);
yp = yp + (t2 * 3.5);
xp = xp / 1000;
yp = yp / 1000;
//NSLog(@"%f, x: %f y: %f wB: %f hB: %f", [_filterCropSlider value], xp, yp, wBuff, hBuff);
[(GPUImageCropFilter *)cropFilter setCropRegion:CGRectMake(xp, yp, [_filterCropSlider value], [_filterCropSlider value])];
self.previewView.image = [cropFilter imageByFilteringImage:previewView.image];
[cropFilter release];
}
if (kuwaharaFilterEnabled){
kuwaharaFilter = [[GPUImageKuwaharaFilter alloc] init];
[(GPUImageKuwaharaFilter *)kuwaharaFilter setRadius:round([_filterKuwaharaSlider value])];
self.previewView.image = [kuwaharaFilter imageByFilteringImage:previewView.image];
[kuwaharaFilter release];
}
if (toonFilterEnabled){
toonFilter = [[GPUImageToonFilter alloc] init];
self.previewView.image = [toonFilter imageByFilteringImage:previewView.image];
[toonFilter release];
}
if (invertFilterEnabled){
invertFilter = [[GPUImageColorInvertFilter alloc] init];
self.previewView.image = [invertFilter imageByFilteringImage:previewView.image];
[invertFilter release];
}
if (pixelFilterEnabled){
pixelFilter = [[GPUImagePixellateFilter alloc] init];
[(GPUImagePixellateFilter *)pixelFilter setFractionalWidthOfAPixel:[_filterPixelSlider value]];
self.previewView.image = [pixelFilter imageByFilteringImage:previewView.image];
[pixelFilter release];
}
if (gammaFilterEnabled){
gammaFilter = [[GPUImageGammaFilter alloc] init];
[(GPUImageGammaFilter *)gammaFilter setGamma:[_filterGammaSlider value]];
self.previewView.image = [gammaFilter imageByFilteringImage:previewView.image];
[gammaFilter release];
}
if (sketchFilterEnabled){
sketchFilter = [[GPUImageSketchFilter alloc] init];
self.previewView.image = [sketchFilter imageByFilteringImage:previewView.image];
[sketchFilter release];
}
if (swirlFilterEnabled){
swirlFilter = [[GPUImageSwirlFilter alloc] init];
[(GPUImageSwirlFilter *)swirlFilter setAngle:[_filterSwirlSlider value]];
self.previewView.image = [swirlFilter imageByFilteringImage:previewView.image];
[swirlFilter release];
}
if (overlayFilterEnabled){
if (![self.overlayImage compare:@""] == NSOrderedSame){
GPUImageOutput<GPUImageInput> *overlayFilter;
if(multiplyBlendEnabled){
overlayFilter = [[GPUImageMultiplyBlendFilter alloc] init];
}else if(overlayBlendEnabled){
overlayFilter = [[GPUImageOverlayBlendFilter alloc] init];
}else if(lightenBlendEnabled){
overlayFilter = [[GPUImageLightenBlendFilter alloc] init];
}else if(darkenBlendEnabled){
overlayFilter = [[GPUImageDarkenBlendFilter alloc] init];
}else if(burnBlendEnabled){
overlayFilter = [[GPUImageColorBurnBlendFilter alloc] init];
}else if(dodgeBlendEnabled){
overlayFilter = [[GPUImageColorDodgeBlendFilter alloc] init];
}else if(screenBlendEnabled){
overlayFilter = [[GPUImageScreenBlendFilter alloc] init];
}else if(differenceBlendEnabled){
overlayFilter = [[GPUImageDifferenceBlendFilter alloc] init];
}else if(subtractBlendEnabled){
overlayFilter = [[GPUImageSubtractBlendFilter alloc] init];
}else if(exclusionBlendEnabled){
overlayFilter = [[GPUImageExclusionBlendFilter alloc] init];
}else if(hardLightBlendEnabled){
overlayFilter = [[GPUImageHardLightBlendFilter alloc] init];
}else if(softLightBlendEnabled){
overlayFilter = [[GPUImageSoftLightBlendFilter alloc] init];
}else{
overlayFilter = [[GPUImageMultiplyBlendFilter alloc] init];
}
UIImage *inputImage = [[[UIImage alloc]init]autorelease];
// The picture is only used for two-image blend filters
inputImage = [UIImage imageNamed:[NSString stringWithFormat:@"preview_%@.jpg", self.overlayImage]];
GPUImagePicture *sourcePreviewPicture = [[GPUImagePicture alloc] initWithImage:inputImage smoothlyScaleOutput:YES];
[sourcePreviewPicture addTarget:overlayFilter];
inputImage = [overlayFilter imageByFilteringImage:self.previewView.image];
if(overlayOpacityFilterEnabled){
GPUImageOutput<GPUImageInput> *overlayOpacityFilter = [[GPUImageAlphaBlendFilter alloc] init];
sourcePreviewPicture = [[GPUImagePicture alloc] initWithImage:self.previewView.image smoothlyScaleOutput:YES];
[sourcePreviewPicture addTarget:overlayOpacityFilter];
[(GPUImageAlphaBlendFilter *)overlayOpacityFilter setMix:[_filterOverlayOpacitySlider value]];
inputImage = [overlayOpacityFilter imageByFilteringImage:inputImage];
[overlayOpacityFilter release];
}
self.previewView.image = inputImage;
[overlayFilter release];
[sourcePreviewPicture release];
}
}
if (borderFilterEnabled){
if (![self.borderImage compare:@""] == NSOrderedSame){
GPUImageOutput<GPUImageInput> *borderFilter;
if ([self.borderImage rangeOfString:@"black"].location == NSNotFound) {
borderFilter = [[GPUImageLightenBlendFilter alloc] init];
} else {
borderFilter = [[GPUImageMultiplyBlendFilter alloc] init];
}
UIImage *inputImage = [[[UIImage alloc]init]autorelease];
// The picture is only used for two-image blend filters
inputImage = [UIImage imageNamed:[NSString stringWithFormat:@"preview_%@.jpg", self.borderImage]];
GPUImagePicture *sourcePreviewPicture = [[GPUImagePicture alloc] initWithImage:inputImage smoothlyScaleOutput:YES];
[sourcePreviewPicture addTarget:borderFilter];
self.previewView.image = [borderFilter imageByFilteringImage:self.previewView.image];
[borderFilter release];
[sourcePreviewPicture release];
}
}
[loopPool drain];
return self.previewView.image;
}