OStack程序员社区-中国程序员成长平台

标题: ios - 使用 UISlider 应用 GPUImage 过滤器 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 06:39
标题: ios - 使用 UISlider 应用 GPUImage 过滤器

我正在使用 GPUImage 滤镜创建一个简单的图像亮度、对比度和饱和度调整工具。该 View 有一个 ImageView 和每个过滤器的 3 个 slider 。起初我以为我会在每个 slider 的值发生变化时应用每个过滤器,但后来我意识到必须链接过滤器。所以我写了下面的代码。这段代码的问题是只对图像应用了亮度滤镜。

- (IBAction)brightnessSliderUISlider *)sender {

    brightnessValue = sender.value;

    UIImage *inputImage = [UIImage imageNamed"2.jpg"];

    GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithImage:inputImage];

    GPUImageBrightnessFilter *brightnessFilter = [GPUImageBrightnessFilter new];
    GPUImageContrastFilter *contrastFilter = [GPUImageContrastFilter new];
    GPUImageSaturationFilter *saturationFilter = [GPUImageSaturationFilter new];

    [brightnessFilter setBrightness:brightnessValue];
    [contrastFilter setContrast:contrastValue];
    [saturationFilter setSaturation:saturationValue];

    [stillImageSource addTarget:brightnessFilter];
    [brightnessFilter addTarget:contrastFilter];
    [contrastFilter addTarget:saturationFilter];
    [brightnessFilter useNextFrameForImageCapture];
    [stillImageSource processImage];

    UIImage *currentFilteredVideoFrame = [brightnessFilter imageFromCurrentFramebuffer];

    imageView.image = currentFilteredVideoFrame;
}

- (IBAction)contrastSliderUISlider *)sender
{
    contrastValue = sender.value;

    UIImage *inputImage = [UIImage imageNamed"2.jpg"];

    GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithImage:inputImage];

    GPUImageBrightnessFilter *brightnessFilter = [GPUImageBrightnessFilter new];
    GPUImageContrastFilter *contrastFilter = [GPUImageContrastFilter new];
    GPUImageSaturationFilter *saturationFilter = [GPUImageSaturationFilter new];

    [brightnessFilter setBrightness:brightnessValue];
    [contrastFilter setContrast:contrastValue];
    [saturationFilter setSaturation:saturationValue];

    [stillImageSource addTarget:brightnessFilter];
    [brightnessFilter addTarget:contrastFilter];
    [contrastFilter addTarget:saturationFilter];
    [brightnessFilter useNextFrameForImageCapture];
    [stillImageSource processImage];

    UIImage *currentFilteredVideoFrame = [brightnessFilter imageFromCurrentFramebuffer];

    imageView.image = currentFilteredVideoFrame;
}


- (IBAction)saturationSliderUISlider *)sender
{
    saturationValue = sender.value;

    UIImage *inputImage = [UIImage imageNamed"2.jpg"];

    GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithImage:inputImage];

    GPUImageBrightnessFilter *brightnessFilter = [GPUImageBrightnessFilter new];
    GPUImageContrastFilter *contrastFilter = [GPUImageContrastFilter new];
    GPUImageSaturationFilter *saturationFilter = [GPUImageSaturationFilter new];

    [brightnessFilter setBrightness:brightnessValue];
    [contrastFilter setContrast:contrastValue];
    [saturationFilter setSaturation:saturationValue];

    [stillImageSource addTarget:brightnessFilter];
    [brightnessFilter addTarget:contrastFilter];
    [contrastFilter addTarget:saturationFilter];
    [brightnessFilter useNextFrameForImageCapture];
    [stillImageSource processImage];

    UIImage *currentFilteredVideoFrame = [brightnessFilter imageFromCurrentFramebuffer];

    imageView.image = currentFilteredVideoFrame;


}

现在我决定以一种更简单的方式来实现整个事情,但我仍然一无所获:

UIImage *inputImage = [UIImage imageNamed"2.jpg"];
    GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithImage:inputImage];

    //Set Brightness to 60
    GPUImageBrightnessFilter *brightnessFilter = [GPUImageBrightnessFilter new];
    [brightnessFilter setBrightness:60.0];

    //Set Contrast to 12
    GPUImageContrastFilter *contrastFilter = [GPUImageContrastFilter new];
    [contrastFilter setContrast:12];


    [brightnessFilter addTarget:contrastFilter];

    [stillImageSource addTarget:contrastFilter];
    [contrastFilter useNextFrameForImageCapture];
    [stillImageSource processImage];

    UIImage *outputImage1 = [contrastFilter imageFromCurrentFramebuffer];
    imageView.image = outputImage1;



Best Answer-推荐答案


您不需要为每个 slider 更改使用相同的添加目标代码。您应该只更改所需过滤器的值并在代码中的其他位置设置所有过滤器;您最终将在其中加载图像。这将使您的代码更容易维护,并使这个问题更短。

现在到实际问题。查看 GPUImage github issue .您已按以下顺序嵌套过滤器:

静止图像 > 亮度 > 对比度 > 饱和度

然后您会从亮度过滤器中获取图像。您需要通过饱和度过滤器获得它。所以,你的链需要是这样的:

静止图像 > 亮度 > 对比度 > 饱和度 > 输出图像

关于ios - 使用 UISlider 应用 GPUImage 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29897180/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4