首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CoreImage的内存泄漏

CoreImage的内存泄漏
EN

Stack Overflow用户
提问于 2014-05-13 18:29:38
回答 2查看 2.7K关注 0票数 0

我已经创建了如下模糊图像:

代码语言:javascript
复制
//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,但仍然在

代码语言:javascript
复制
CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];

我也被推荐了所有其他相关的问题,并实现了提供的答案仍然得到内存泄漏

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

代码语言:javascript
复制
context = nil;
outputImage = nil;
gaussianBlurFilter = nil;
viewImage = nil;

在此之前

代码语言:javascript
复制
CGImageRelease(cgimg);

尽管如此,它仍然显示了内存泄漏,说明如下。ARC已打开,ios版本为6.1和7

责任库是: CoreImage责任调用者是: CI::GLESContext::program_for_name(__CFString const*)

有人能建议我如何解决这个内存泄漏问题吗?

提前感谢您的帮助。

EN

回答 2

Stack Overflow用户

发布于 2014-05-13 18:35:53

所有的核心框架都使用C语言,而不是Objective-C。你必须手动进行内存管理,ARC在这方面帮不了你太多,除非将一些(免费桥接的)引用的所有权从Core转移到Objective-C对象。你真的应该阅读Apple documentation来理解你需要做什么。

简而言之,设置对nil的引用(在C中应该是NULL )会给您留下一个悬空指针和内存泄漏。

票数 1
EN

Stack Overflow用户

发布于 2014-08-18 19:20:08

问题就是常量调用

代码语言:javascript
复制
[CIContext contextWithOptions:nil]

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

代码语言:javascript
复制
@property (retain, nonatomic) CIContext *context;

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

代码语言:javascript
复制
if( self.context == nil ) {
    self.context = [CIContext contextWithOptions:nil];
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23628783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档