GPUImage 花时间应用过滤器

GPUImage Take time to apply filter

我正在 GPUImage 图书馆工作。我已设置特定过滤器将应用于 Collectionview 的 didselectitematindexpath 方法。这是我基于 GPUImage 库的第一个项目。我能够成功地在 GPUImageview 上应用过滤器,但是应用过滤器需要很多时间。请指导我,我怎样才能快速申请。

这是我的代码,

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
       [filter removeAllTargets];

    if (indexPath.row==0) {
        NSLog(@"Normal Filter");
    }
    else if (indexPath.row==1)
    {
        filter = [[GPUImageSepiaFilter alloc] init];

    }
    else if (indexPath.row==2)
    {
        filter = [[GPUImagePixellateFilter alloc] init];

    }
    else if (indexPath.row==3)
    {
        filter = [[GPUImagePixellatePositionFilter alloc] init];

    }
    else if (indexPath.row==4)
    {
        filter = [[GPUImagePolkaDotFilter alloc] init];

    }
    else if (indexPath.row==5)
    {
        filter = [[GPUImageHalftoneFilter alloc] init];

    }
    else if (indexPath.row==6)
    {
        filter = [[GPUImageCrosshatchFilter alloc] init];

    }
    else if (indexPath.row==7)
    {
        filter = [[GPUImageColorInvertFilter alloc] init];

    }
    else if (indexPath.row==8)
    {
        filter = [[GPUImageGrayscaleFilter alloc] init];

    }
    else if (indexPath.row==9)
    {
        filter = [[GPUImageMonochromeFilter alloc] init];
        [(GPUImageMonochromeFilter *)filter setColor:(GPUVector4){0.0f, 0.0f, 1.0f, 1.f}];
    }
    else if (indexPath.row==10)
    {
        filter = [[GPUImageFalseColorFilter alloc] init];

    }
    else if (indexPath.row==11)
    {
        filter = [[GPUImageSoftEleganceFilter alloc] init];

    }
    else if (indexPath.row==12)
    {
        filter = [[GPUImageMissEtikateFilter alloc] init];

    }
    else if (indexPath.row==13)
    {
        filter = [[GPUImageAmatorkaFilter alloc] init];

    }
    else if (indexPath.row==14)
    {
        filter = [[GPUImageSaturationFilter alloc] init];
       [(GPUImageSaturationFilter *)filter setSaturation:2.0];
    }
    else if (indexPath.row==15)
    {
        filter = [[GPUImageContrastFilter alloc] init];
        [(GPUImageContrastFilter *)filter setContrast:1.5];


    }
    else if (indexPath.row==16)
    {
        filter = [[GPUImageBrightnessFilter alloc] init];
        [(GPUImageBrightnessFilter *)filter setBrightness:0.6];


    }
    else if (indexPath.row==17)
    {
        filter = [[GPUImageLevelsFilter alloc] init];

    }
    else if (indexPath.row==18)
    {
        filter = [[GPUImageRGBFilter alloc] init];

    }
    else if (indexPath.row==19)
    {
        filter = [[GPUImageHueFilter alloc] init];

    }
    else if (indexPath.row==20)
    {
        filter = [[GPUImageWhiteBalanceFilter alloc] init];

    }
    else if (indexPath.row==21)
    {
        filter = [[GPUImageExposureFilter alloc] init];

    }
    else if (indexPath.row==22)
    {
        filter = [[GPUImageSharpenFilter alloc] init];

    }
    else if (indexPath.row==23)
    {
        filter = [[GPUImageUnsharpMaskFilter alloc] init];

    }
    else if (indexPath.row==24)
    {
        filter = [[GPUImageGammaFilter alloc] init];

    }
    else if (indexPath.row==25)
    {
        filter = [[GPUImageToneCurveFilter alloc] init];
        [(GPUImageToneCurveFilter *)filter setBlueControlPoints:[NSArray arrayWithObjects:[NSValue valueWithCGPoint:CGPointMake(0.0, 0.0)], [NSValue valueWithCGPoint:CGPointMake(0.5, 0.5)], [NSValue valueWithCGPoint:CGPointMake(1.0, 0.75)], nil]];
    }
    else if (indexPath.row==26)
    {
        filter = [[GPUImageHighlightShadowFilter alloc] init];

    }
    else if (indexPath.row==27)
    {
        filter = [[GPUImageHazeFilter alloc] init];

    }
    else if (indexPath.row==28)
    {
        filter = [[GPUImageAverageColor alloc] init];

    }
    else if (indexPath.row==29)
    {
        filter = [[GPUImageLuminosity alloc] init];

    }
    else if (indexPath.row==30)
    {
        filter = [[GPUImageHistogramFilter alloc] initWithHistogramType:kGPUImageHistogramRGB];

    }
    else if (indexPath.row==31)
    {
        filter = [[GPUImageLuminanceThresholdFilter alloc] init];

    }
    else if (indexPath.row==32)
    {
        filter = [[GPUImageAdaptiveThresholdFilter alloc] init];

    }
    else if (indexPath.row==33)
    {
        filter = [[GPUImageAverageLuminanceThresholdFilter alloc] init];

    }
    else if (indexPath.row==34)
    {
        filter = [[GPUImageCropFilter alloc] initWithCropRegion:CGRectMake(0.0, 0.0, 1.0, 0.25)];
    }


    [sourcePicture addTarget:filter];
    [filter addTarget:self.vwGPUImagview];
    [sourcePicture processImage];


}

输出:-

编辑:- 当我写 [filter removeAllTarget] 时,一些过滤器不起作用。如果我删除它然后它完全工作。我的错误在哪里?

编辑 2:- Demo Link

根据提供的代码,您在这里做的一些事情确实会减慢速度。首先,您有一个显然无用的 GPUImageVideoCamera,您重新创建了多次。那个可以去掉。

其次,每次要显示单元格时,您都会基于 UIImage 创建一个新的 GPUImagePicture。这样做真的很慢。相反,尝试使用单个共享图像并在显示单元格时将其换出。此外,使用 -forceProcessingAtSize: 将过滤器大小限制为单元格中缩略图的大小。过滤大图毫无意义,只显示小缩略图。

最后,正如我在上面指出的那样,每次换入新滤镜时,都会移除滤镜的输出,但不会移除图像的输出。这意味着您交换到的每个滤镜都会添加为图像的输出,但不会被删除。您的图像将建立越来越多的输出过滤器列表,并且处理速度会越来越慢。在删除以前过滤器的所有输出的同时,从图片中删除所有输出。

您也可以在您的滤镜上使用 -forceProcessingAtSize: 将它们限制为仅显示完整显示的大小,这可能仍小于您的源图像。