Reputation: 188
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
Upvotes: 3
Views: 5160
Reputation: 10754
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):
alloc
, init
, copy
, new
create
, or by functions containing Create
or Copy
.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.
Upvotes: 3