ios - 如何减少 GPUImageGaussianSelectiveBlurFilter 效果中的内存消耗?

标签 ios gpuimage

我正在使用GPUImage框架实现GPUImageGaussianSelectiveBlurFilter效果。

现在GPUImageGaussianSelectiveBlurFilter效果已经实现了,但是内存消耗太大了。我知道通过forceProcessingAtSize方法可以减少一些内存消耗,但是减少内存消耗太少了.

如果不调用processImage方法,内存消耗会减少很多,那么如何使用其他方法来代替呢?

如何减少内存消耗?

#import "ViewController.h"
#import "GPUImage.h"
@interface ViewController ()
{
  UIImageView *captureImageView;
  UIScrollView *scrollView;
  GPUImageView *imageViewGpu;
  GPUImageGaussianSelectiveBlurFilter *filter;
  BOOL filterFalg;
}
@end

@implementation ViewController

-(void)viewDidLoad
{

[super viewDidLoad];
self.view.backgroundColor = [UIColor clearColor];

// create scrollView
scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
[self.view addSubview:scrollView];
scrollView.minimumZoomScale = 1.0;
scrollView.maximumZoomScale = 3.0;
scrollView.delegate = self;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
scrollView.bounces = NO;

// create imageView
captureImageView = [[UIImageView alloc] init];
captureImageView.frame = self.view.frame;
captureImageView.image = [UIImage imageNamed:@"image.jpg"];

// add filter to imagePic
GPUImagePicture *imagePic = [[GPUImagePicture alloc] initWithImage:captureImageView.image smoothlyScaleOutput:YES];
filter = [[GPUImageGaussianSelectiveBlurFilter alloc] init];
[imagePic addTarget:filter];

// create GPUImageView
imageViewGpu = [[GPUImageView alloc] initWithFrame:self.view.frame];
imageViewGpu.userInteractionEnabled = YES;
imageViewGpu.multipleTouchEnabled = YES;
imageViewGpu.backgroundColor = [UIColor clearColor];
imageViewGpu.fillMode = kGPUImageFillModePreserveAspectRatio;
[scrollView addSubview:imageViewGpu];


if (imageViewGpu.frame.size.height>imageViewGpu.frame.size.width) {
    [filter setAspectRatio:imageViewGpu.frame.size.height/imageViewGpu.frame.size.width];
}
else
{
    [filter setAspectRatio:imageViewGpu.frame.size.width/imageViewGpu.frame.size.height];
}


[filter setExcludeCircleRadius:0.0f];
[filter setExcludeCirclePoint:CGPointMake(0.5f, 0.5f)];
[filter setBlurRadiusInPixels:10];
[filter addTarget:imageViewGpu];

[imagePic processImage];

}  

 @end

最佳答案

如果您所做的只是将图像显示到屏幕上,请在链中的第一个过滤器上使用 -forceProcessingAtSize:。对于大型输入图像,这将显着减少它们在内存中的大小。

另外,不要加载相同的图像两次,一次用于 UIImageView,一次用于 GPUImagePicture。相反,我建议删除 UIImageView,并通过添加 GPUImageView 作为 GPUImagePicture 的并行目标,将图像的未过滤版本显示到 GPUImageView。这将为大图片节省大量内存。

最后,您将在 ARC 下使用上述代码发生崩溃,因为您没有在任何地方保留 GPUImagePicture。您需要将 GPUImagePicture 设置为类的实例变量或属性,否则它将在上述方法完成后立即被释放。如果你不使用 ARC,你就会在上面的所有地方泄漏内存(这可能是你问题的一部分)。

关于ios - 如何减少 GPUImageGaussianSelectiveBlurFilter 效果中的内存消耗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25028357/

相关文章:

ios - 二维 NSArray 中的对象数

ios - 选择 时如何从 TableView 中隐藏行

ios - 强制提前调用 iOS 设备上的后台任务到期处理程序以进行测试

ios - 在 iOS 中使用核心数据提供初始数据

ios - GPUImageLookupFilter 不工作

ios - 提高 VNDetectHumanBodyPoseRequest 的 body 跟踪性能

ios - GPUImage glsl 正弦波 photoshop 效果

android - 在 Android 上录制视频时添加覆盖

ios - GPUImage - 尝试在混合过于频繁时过度释放帧缓冲区

ios - 如何在 iOS 中移动 slider 时在图像上应用亮度、对比度、 Gamma ?