如何减少GPUImageGaussianSelectiveBlurFilter效果中的内存消耗? [英] How to reduce the memory consumption in GPUImageGaussianSelectiveBlurFilter effect?

查看:113
本文介绍了如何减少GPUImageGaussianSelectiveBlurFilter效果中的内存消耗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using GPUImage framework implementation GPUImageGaussianSelectiveBlurFilter effect.

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

Now GPUImageGaussianSelectiveBlurFilter effect has been achieved,but there is too much memory consumption.I know through the forceProcessingAtSize method can reduce some memory consumption, but reduce memory consumption too little.

如果未调用processImage方法,则内存消耗将减少很多,那么如何使用另一种方法替换它呢?

If the processImage method is not called, the memory consumption will reduce a lot, so how to use another method to replace it?

如何减少内存消耗?

#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:.对于大型输入图像,这将大大减少其在内存中的大小.

If all you're doing is displaying the image to the screen, use -forceProcessingAtSize: on the first filter in your chain. For large input images, that will dramatically reduce their size in memory.

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

Also, don't load the same image twice, once for your UIImageView and once for your GPUImagePicture. Instead, I recommend removing the UIImageView and displaying an unfiltered version of your image to a GPUImageView by adding a GPUImageView as a parallel target of your GPUImagePicture. That will save a lot of memory right there for a large picture.

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

Finally, you're going to get crashes with the above code under ARC, because you're not holding on to your GPUImagePicture anywhere. You need to set your GPUImagePicture as an instance variable or property of your class, or it will be deallocated the instant the above method completes. If you're not using ARC, you're leaking memory all over the place in the above (which could be part of your problem).

这篇关于如何减少GPUImageGaussianSelectiveBlurFilter效果中的内存消耗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆