本文介绍了CoreImage的内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了如下模糊图像:

I have created blur image as below:

//Blur the UIImage
CIImage *imageToBlur = [CIImage imageWithCGImage:viewImage.CGImage];
CIFilter *gaussianBlurFilter = [CIFilter filterWithName: @"CIGaussianBlur"];
[gaussianBlurFilter setValue:imageToBlur forKey: @"inputImage"];
[gaussianBlurFilter setValue:[NSNumber numberWithFloat: 2] forKey: @"inputRadius"];
CIImage *resultImage = [gaussianBlurFilter valueForKey: @"outputImage"];
CIImage *finalImage = [resultImage imageByCroppingToRect:CGRectMake(0, 10, self.view.bounds.size.width, self.view.bounds.size.height)];

//create UIImage from filtered image
UIImage *blurrredImage = [[UIImage alloc] initWithCIImage:finalImage];

CIFilter *colorMatrixFilter = [CIFilter filterWithName:@"CIColorMatrix"];
[colorMatrixFilter setDefaults];
[colorMatrixFilter setValue:finalImage forKey:kCIInputImageKey];
[colorMatrixFilter setValue:[CIVector vectorWithX:0.25 Y:0 Z:0 W:0] forKey:@"inputRVector"];
[colorMatrixFilter setValue:[CIVector vectorWithX:0 Y:0.35 Z:0 W:0] forKey:@"inputGVector"];
[colorMatrixFilter setValue:[CIVector vectorWithX:0 Y:0 Z:0.4 W:0] forKey:@"inputBVector"];
[colorMatrixFilter setValue:[CIVector vectorWithX:0 Y:0 Z:0 W:1] forKey:@"inputAVector"];

// Get the output image recipe
CIImage *outputImage = [colorMatrixFilter outputImage];

// Create the context and instruct CoreImage to draw the output image recipe into a CGImage
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
blurrredImage = [UIImage imageWithCGImage:cgimg];


CGImageRelease(cgimg);

//Place the UIImage in a UIImageView
UIImageView *newView = [[UIImageView alloc] initWithFrame:self.view.bounds];
newView.image = blurrredImage;

//insert blur UIImageView below transparent view inside the blur image container
[blurContainerView insertSubview:newView belowSubview:transparentView];

我已经发布了CGImageRef,但是仍然出现内存泄漏

I have released the CGImageRef, but still getting the memory leak at

CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];

我还转介了所有其他相关问题,并实施提供的答案仍然会导致内存泄漏

I am also referred all other related questions and and implement the provided answers still getting memory leak

使用@autoreleasepool {}并尝试将对象设置为nil

Used @autoreleasepool { } and try to set object to nil

context = nil;
outputImage = nil;
gaussianBlurFilter = nil;
viewImage = nil;

之前

CGImageRelease(cgimg);

它仍然显示内存泄漏,说明如下. ARC已打开,并且ios版本为6.1和7

Still it shows the memory leak and description is as below. ARC is on and ios version is 6.1 and 7

负责的图书馆是:CoreImage负责的呼叫者是:CI :: GLESContext :: program_for_name(__ CFString const *)

Responsible Library is : CoreImageResponsible Caller is : CI::GLESContext::program_for_name(__CFString const*)

有人可以建议我如何解决此内存泄漏吗?

Can anybody please suggest me how can i resolved this memory leak?

非常感谢您的帮助.

推荐答案

问题可能是常量调用

[CIContext contextWithOptions:nil]

我建议在您的课程中创建一个CIContext属性:

I would recommend creating a property of CIContext in your class:

@property (retain, nonatomic) CIContext *context;

并且仅在上下文为零时实例化它

and only instantiate it when your context is nil

if( self.context == nil ) {
    self.context = [CIContext contextWithOptions:nil];
}

这篇关于CoreImage的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 06:33