Memory leak when filtering with Core image

自古美人都是妖i 提交于 2020-01-04 02:43:07

问题


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 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.



来源:https://stackoverflow.com/questions/11222825/memory-leak-when-filtering-with-core-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!