根据您应用的具体情况,您可能还需要考虑 GPUImageFilterPipeline 类。
它将负责添加和删除 Brad 引用的所有干预目标。
如果这些过滤器的重复设置/拆卸对您来说是个问题,但在您的课程生命周期内将它们保留在内存中却不是,那么您可能会喜欢管道。
根据您提供的大致内容,它可能看起来像这样:
- (void)configureSomeArraysOfFilters {
_setNumberOne = [[NSMutableArray alloc] init]; //make sure these arrays are at least scoped to the class, if not actual @properties
GPUImageKuwaharaFilter* kuwahara = [[GPUImageKuwaharaFilter alloc] init];
[kuwahara setRadius:5];
GPUImageGrayscaleFilter* gray = [[GPUImageGrayscaleFilter alloc] init];
[_setNumberOne addObject:kuwahara];
[_setNumberOne addObject:gray];
_setNumberTwo = [[NSMutableArray alloc] init];
GPUImageGrayscaleFilter* otherGray = [[GPUImageGrayscaleFilter alloc] init];
GPUImageGaussianBlurFilter* blur = [[GPUImageGaussianBlurFilter alloc] init];
[blur setBlurSize:3];
GPUImageColorInvertFilter* invert = [[GPUImageColorInvertFilter alloc] init];
[_setNumberTwo addObject:otherGray];
[_setNumberTwo addObject:blur];
[_setNumberTwo addObject:invert];
}
- (void)configureAnEmptyPipeline {
if (_samplePipeline == nil) {
GPUImageFilter* passthrough = [[GPUImageFilter alloc] init];
NSArray* empty = [NSArray arrayWithObjects:passthrough, nil];
_samplePipeline = [[GPUImageFilterPipeline alloc] initWithOrderedFilters:empty input:videoCamera output:_filteredVideoView];
[videoCamera startCameraCapture];
}
}
- (void)updateFilterPipeline:(NSInteger)style {
switch (style) {
case 1:
[_samplePipeline replaceAllFilters:_setNumberOne];
break;
case 2:
[_samplePipeline replaceAllFilters:_setNumberTwo];
//add as many more cases as you have defined Arrays full of filters for
default:
break;
}
}
然而,我最喜欢的管道用例是当我在运行时动态创建过滤器集,然后将它们切换为行动。它允许我简单地按顺序存储过滤器,然后将它们传递到管道,而不必每次都指定每个过滤器之间的所有目标。
它并不适用于所有情况,但 GPUImageFilterPipeline 在某些情况下可能非常有用。