如何为UIImage GPUImage添加多个过滤器?

时间:2015-02-04 23:30:42

标签: ios objective-c filter gpuimage brightness

我注意到很多人都在询问有关将过滤器与GPUImage相关联的问题。我无法弄清楚如何简洁地做到这一点。终于让它今晚工作了。只是想分享我的代码,以便人们可以链接到解决方案。

1 个答案:

答案 0 :(得分:2)

UIImage *faceImage = [UIImage imageNamed:@"469453586_640.jpg"];
UIImageView *face = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, faceImage.size.width/2.0, faceImage.size.height/2.0)];
[face setImage:faceImage];
[self.view addSubview:face];

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

GPUImageBrightnessFilter *brightnessFilter = [[GPUImageBrightnessFilter alloc] init];
[brightnessFilter setBrightness:.15];
GPUImageGrayscaleFilter *grayscaleFilter = [[GPUImageGrayscaleFilter alloc] init];

GPUImagePosterizeFilter *posterizeFilter = [[GPUImagePosterizeFilter alloc] init];
[posterizeFilter setColorLevels:1];


[stillImageSource addTarget:brightnessFilter];
[brightnessFilter addTarget:grayscaleFilter];
[grayscaleFilter addTarget:posterizeFilter];

// these need to be changed if you change the order of your filters
//    [brightnessFilter useNextFrameForImageCapture];
//    [grayscaleFilter useNextFrameForImageCapture];
[posterizeFilter useNextFrameForImageCapture];

[stillImageSource processImage];

[face setImage: [posterizeFilter imageFromCurrentFramebuffer]];