使用Core映像过滤时发生内存泄漏 [英] Memory leak when filtering with Core image
问题描述
所以我一直在使用核心图像在图像上应用过滤器,一切都很好,除了当我试图一遍又一遍地应用相同的过滤器而退出时,我猜它是内存泄漏的。
So I've been using core image to apply filters on images, everything is good except when I try to apply the same filter over and over again the application just quits, I guess its a memory leak.
代码如下:
-(UIImage *) applyFilter: (UIImage*) picture
{
UIImageOrientation originalOrientation = picture.imageOrientation;
CGFloat originalScale = picture.scale;
CIImage *beginImage = [CIImage imageWithCGImage:picture.CGImage];
CIContext *context = [CIContext contextWithOptions:nil];
CIFilter *filter = [CIFilter filterWithName:@"CISepiaTone"
keysAndValues: kCIInputImageKey, beginImage,
@"inputIntensity", [NSNumber numberWithFloat:0.8], nil];
CIImage *outputImage = [filter outputImage];
CGImageRef cgimg =
[context createCGImage:outputImage fromRect:[outputImage extent]];
UIImage *newImg = [UIImage imageWithCGImage:cgimg scale:originalScale orientation:originalOrientation];
beginImage = nil;
context = nil;
filter = nil;
outputImage = nil;
cgimg = nil;
[beginImage release];
[context release];
[filter release];
[outputImage release];
//CGImageRelease(CGImageRef) method.
CGImageRelease(cgimg);
return newImg;
}
要过滤,我只是做
UIImage *ima = [self.filter applyFilter:self.imageView.image];
imageView.image = ima ;
applyFilter是我创建的Filter类的方法
The applyFilter is a method of Filter class that I created
推荐答案
在调用 release
之前,将变量设置为nil,所以 release
无效。但是无论如何,您都不应该发布大部分内容。您只需要释放您创建的对象(我希望下面的列表是完整的):
You set variables to nil before you call release
, so the release
has no effect. But you should not release most of the stuff anyway. You only need to release objects that you created (I hope the following list is complete):
- Objective-由以
alloc
,init
,copy $ c开始的方法返回的C对象$ c>,
新
- 由Objective-C方法返回的基础对象,它们以
create $ c开头$ c>或包含
Create
或Copy
的函数。
- Objective-C objects that were returned by methods starting with
alloc
,init
,copy
,new
- Foundation objects returned by Objective-C methods starting with
create
, or by functions containingCreate
orCopy
.
删除这些行,应该没问题:
Delete these lines and it should be fine:
beginImage = nil;
context = nil;
filter = nil;
outputImage = nil;
cgimg = nil;
[beginImage release];
[context release];
[filter release];
[outputImage release];
您需要保持该行 CGImageRelease(cgimg);
因为用于获取 cgimg
的方法包含 create
–创建它,然后释放它。
You need to keep the line CGImageRelease(cgimg);
because the method used to get cgimg
contains create
– you create it, you release it.
这篇关于使用Core映像过滤时发生内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!