1

我注意到我使用 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];

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

我究竟做错了什么?一旦我不再需要它,如何释放内存?

4

1 回答 1

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,您绝对应该考虑一下

于 2013-11-20T09:19:40.690 回答