与核心图像过滤时内存泄漏(Memory leak when filtering with Core

2019-09-18 14:27发布

所以,我一直在使用的核心图像对图像应用滤镜,一切都只是很好,当我尝试一遍又一遍地将相同的过滤器的应用程序只是退出,我想它的内存泄漏。

下面的代码:

-(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是我创建的过滤器类的方法

Answer 1:

你给变量设置为零打电话之前release ,因此release没有任何影响。 但你不应该释放大部分的东西反正。 你只需要释放你创建 (我希望下面的列表是完整的)对象:

  • 第一步是用方法所返回的Objective-C对象allocinitcopynew
  • Foundation对象的Objective-C的方法返回开始create ,或通过含有功能CreateCopy

删除这些行,它应该是罚款:

beginImage = nil;
context = nil;
filter = nil;
outputImage = nil;
cgimg = nil;
[beginImage release];
[context release];
[filter release];
[outputImage release];

你需要保持线路CGImageRelease(cgimg); 因为用来获取方法cgimg包含create -创建它,你松开。



文章来源: Memory leak when filtering with Core image