【问题标题】:GPUImageFilter vs CIFilter (memory issue with GPUImage)GPUImageFilter 与 CIFilter(GPUImage 的内存问题)
【发布时间】:2014-01-14 13:05:09
【问题描述】:

GPUImage 是否存在内存问题?下面有两个不同的 Vignette 滤镜效果代码。第一个(Apple 的 CI 过滤器)使用 19 MB 内存,而 GPUImage 使用超过 75 MB。我的代码有什么问题?

带有 CIFilter 的晕影过滤器

CIImage *ciImage = [[CIImage alloc] initWithImage:image];
CIFilter *filter = [CIFilter filterWithName:@"CIVignetteEffect" keysAndValues:kCIInputImageKey, ciImage, nil];

CIContext *context = [CIContext contextWithOptions:nil];
CIImage *outputImage = [filter outputImage];
CGImageRef cgImage = [context createCGImage:outputImage fromRect:[outputImage extent]];

UIImage *result = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);

return result;

这是 GPUImage 版本:

GPUImageFilter * f = [[GPUImageVignetteFilter alloc] init];
UIImage *result = [f imageByFilteringImage:image];

return result;

我在我的项目中使用“ARC”。你有什么主意吗?我应该怎么做才能“释放” GPUImage 过滤器内存?

【问题讨论】:

    标签: objective-c gpuimage cifilter


    【解决方案1】:

    对于上述代码中的 GPUImage 方法,内存中会简要地存在一个图像的四个副本:原始 UIImage 及其字节,此图像上传到 GPU 上的纹理,过滤操作的结果,以及由于该操作而创建的 UIImage 及其字节。

    上面使用的便捷方法可能很简单,但它们在减少内存压力方面并不是最好的。相反,我会尝试以下方法:

    GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithImage:image smoothlyScaleOutput:NO];
    GPUImageVignetteFilter *vignetteFilter = [[GPUImageVignetteFilter alloc] init];
    [stillImageSource addTarget:vignetteFilter];
    [stillImageSource prepareForImageCapture];
    [stillImageSource processImage];
    UIImage *result = [vignetteFilter imageFromCurrentlyProcessedOutput];
    

    根据原始 UIImage 的创建方式,您可能希望将第一行与原始 UIImage 的创建一起包含在 @autoreleasepool 中。第一行之后不再需要原始的 UIImage,因此在任何给定时间,内存中最多应该有两个图像。

    -prepareForImageCapture 位是一种优化,它使由此返回的 UIImage 与过滤器的输出纹理共享内存映射。这消除了最后对图像副本之一的需要,但它确实将过滤器锁定到 UIImage 并且如果您尝试重用该过滤器或 UIImage 可能会导致奇怪的行为。我也不完全确定当过滤器在其内存映射的 UIImage 之前被释放时所有内存都被释放,因此在该方法结束时将该图像编码为 JPEG 或 PNG 并且只传回NSData 表示。

    【讨论】:

    • 谢谢布拉德,我会按照你的建议实施并在这里写下结果。顺便说一句,很棒的图书馆。
    猜你喜欢
    • 2015-08-17
    • 1970-01-01
    • 1970-01-01
    • 2018-11-27
    • 2015-02-06
    • 1970-01-01
    • 2013-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多