2

我想使用下面的代码在案例 1案例 2中所示的两个过滤器链之间切换。当我最初选择任何一种情况时,输出看起来都是正确的。但是,当我切换到另一个过滤器链时,输出会在当前和之前的过滤器链之间闪烁。切换过滤器链的推荐方法是什么?

-(void) updateFilter:(NSInteger) style {
switch (style) {
    case 1:
        [kuwahara setRadius:5];
        [videoCamera addTarget:kuwahara];
        [kuwahara addTarget:grayscale];
        [grayscale addTarget:filteredVideoView];
        break;
    case 2:    
        [videoCamera addTarget:grayscale];
        [blur setBlurSize:3];
        [grayscale addTarget:blur];
        [blur addTarget:colorinvert];
        [colorinvert addTarget:filteredVideoView];
        break;
    default:
        [videoCamera addTarget:filteredVideoView];
        break;
}
[videoCamera startCameraCapture];
}  
4

1 回答 1

11

根据您应用的具体情况,您可能还需要考虑 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 在某些情况下可能非常有用。

于 2013-10-15T03:28:25.457 回答