GPUImage花时间应用过滤器

时间:2016-05-03 14:19:35

标签: ios objective-c gpuimage

我正在GPUImage图书馆工作。我已经设置了特定的过滤器将应用于Collectionview的didselectitematindexpath方法。这是我的第一个基于GPUImage库的项目。我成功地能够在GPUImageview上应用Filter,但是需要花费大量时间来应用过滤器。请指导我,如何快速申请。

这是我的代码,

-(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];


}

输出: -

enter image description here

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

编辑2: - Demo Link

1 个答案:

答案 0 :(得分:5)

根据提供的代码,您在这里做了一些事情,这将真正减慢这一点。首先,您有一个显然无用的GPUImageVideoCamera,您可以重复多次。这可以删除。

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

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

您还可以在过滤器上使用-forceProcessingAtSize:将其限制为仅显示整个显示屏的尺寸,这可能仍然小于您的源图像。