简介:我们常常遇到需要叠加使用滤镜的业务需求,比如一个相机app里既需要素描效果的同时又需要周围阴影效果以突出中间图像.这就需要本文介绍的有关滤镜组合使用的内容.
GPUImage 中关于滤镜组合使用有两种方法:
- GPUImageFilterGroup与相应filter(GPUImageFilter)相结合
- 将需要使用的filter相累加
第二种方法以后另讲,这里主要讲一下有关使用GPUImageFilterGroup来处理滤镜组合的问题.
GPUImageFilterGroup是GPUImageOutput的子类,其中有一个可变数组属性filters,用来存储相应的滤镜.
@property(readwrite,nonatomic, strong)GPUImageOutput<GPUImageInput> *terminalFilter;
@property(readwrite,nonatomic, strong)NSArray *initialFilters;
- (void)viewDidLoad {
[super viewDidLoad];
_videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack];
_videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
_filter = [[GPUImageFilterGroup alloc] init];
GPUImageSketchFilter *sketchFilter = [[GPUImageSketchFilter alloc] init];
[_filter addFilter:sketchFilter];
GPUImageVignetteFilter *vignetteFilter = [[GPUImageVignetteFilter alloc] init];
[_filter addFilter:vignetteFilter];
[sketchFilter addTarget:vignetteFilter];
[_filter setInitialFilters:[NSArray arrayWithObject:sketchFilter]];
[_filter setTerminalFilter:vignetteFilter];
[_videoCamera addTarget:_filter];
_videoCamera.runBenchmark = YES;
_filterView = [[GPUImageView alloc] initWithFrame:self.view.bounds];
self.view = _filterView;
[_filter addTarget:_filterView];
[_videoCamera startCameraCapture];
_mButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
[_mButton setTitle:@"录制" forState:UIControlStateNormal];
[_mButton sizeToFit];
[self.view addSubview:_mButton];
[_mButton addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
}
这样就将素描效果与晕影效果叠加作用在一起了.最后录制视频截图效果如下