GPUImage内存使用情况

时间:2013-07-31 13:35:27

标签: objective-c memory gpuimage

我注意到我使用GPUImage越多,我的应用程序占用的内存越多(使用Instruments监视内存使用情况)。

举个例子,我用不同的方法使用每个过滤器:

 (UIImage*)ToonFilter:(UIImage*)theImage
    {
    GPUImageSmoothToonFilter *smoothToonFilter = [[GPUImageSmoothToonFilter alloc] init];
    [smoothToonFilter setTexelHeight:0.0025];
    [smoothToonFilter setTexelWidth:0.0025];
    return [smoothToonFilter imageByFilteringImage:theImage];
    }

    (UIImage*)SketchFilter:(UIImage*)theImage
    {
    GPUImageSketchFilter *sketchFilter = [[GPUImageSketchFilter alloc] init];
    [sketchFilter setTexelHeight:0.003];
    [sketchFilter setTexelWidth:0.003];
    return [sketchFilter imageByFilteringImage:theImage];
    }

    (UIImage*)PixellateFilter:(UIImage*)theImage
    {
    GPUImagePixellateFilter *pixellateFilter = [[GPUImagePixellateFilter alloc] init];
    [pixellateFilter setFractionalWidthOfAPixel:0.01;
    return [pixellateFilter imageByFilteringImage:theImage];
    }

这就是我使用这些过滤器的方法(testImage是一个UIImage):

testImage = [self SketchFilter:testImage];
testImage = [self PixellateFilter:testImage];

如果我只是一遍又一遍地浏览这些过滤器而不做任何其他事情,那么该应用程序会占用越来越多的内存。

我做错了什么?一旦我不再需要它,我怎么才能释放内存?

1 个答案:

答案 0 :(得分:0)

每次调用这些函数时,都会不断分配新的过滤器。在视图控制器等中创建一次,然后将它们发送到您的函数,如下面的示例代码(以及GPUImagePicture)。另外,不要忘记在过滤器上调用[removeAllTargets],有时候他们在有目标时不会从内存中删除

(UIImage*)PixellateFilter:(UIImage*)theImage withPixellateFilter:(GPUImagePixellateFilter *) pixellateFilter andStaticPicture:(GPUImagePicture *)staticPicture

并在从过滤器获取UIImage之前使用[filter prepareForImageCapture]

例如,您可以像

一样构建您的函数
(UIImage*)PixellateFilter:(UIImage*)theImage withPixellateFilter:(GPUImagePixellateFilter *) pixellateFilter andStaticPicture:(GPUImagePicture *)staticPicture{
    [staticPicture removeAllTargets];
    UIImage __block *imageToReturn;
    [staticPicture addTarget:pixellateFilter];
    [staticPicture processImageWithCompletionHandler:^{
            [pixellateFilter prepareForImageCapture];
            imageToReturn = [pixellateFilter imageFromCurrentlyProcessedOutput];
            [pixellateFilter removeAllTargets];
            pixellateFilter = nil;
    }];
    return imageToReturn;
}

我在我的应用程序中使用GPUImageView,它允许您直接使用GPUImageFilters和GPUImagePictures而无需获取UIImage,您一定要考虑它