问题
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.
Here's the code:
-(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;
}
And to filter I simply do
UIImage *ima = [self.filter applyFilter:self.imageView.image];
imageView.image = ima ;
The applyFilter is a method of Filter class that I created
回答1:
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-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];
You need to keep the line CGImageRelease(cgimg);
because the method used to get cgimg
contains create
– you create it, you release it.
来源:https://stackoverflow.com/questions/11222825/memory-leak-when-filtering-with-core-image