GPUImage混合滤镜可以用GPUImageFilterGroup和GPUImageFilterPipeline
GPUImageFilterGroup用法
+ (UIImage *)applyGroupFilter:(UIImage *)image //实际是把filter一个个链接起来的
{
GPUImagePicture *pic=[[GPUImagePicture alloc]initWithImage:image];
GPUImageFilterGroup *filterGroup = [[GPUImageFilterGroup alloc] init];
// 添加 filter
/**
原理:
1. filterGroup(addFilter) 滤镜组添加每个滤镜
2. 按添加顺序(可自行调整)前一个filter(addTarget) 添加后一个filter
3. filterGroup.initialFilters = @[第一个filter]];
4. filterGroup.terminalFilter = 最后一个filter;
*/
GPUImageRGBFilter *filter1 = [[GPUImageRGBFilter alloc] init];
GPUImageToonFilter *filter2 = [[GPUImageToonFilter alloc] init];
GPUImageColorInvertFilter *filter3 = [[GPUImageColorInvertFilter alloc] init];
GPUImageSepiaFilter *filter4 = [[GPUImageSepiaFilter alloc] init];
[filterGroup addFilter:filter1];
[filterGroup addFilter:filter2];
[filterGroup addFilter:filter3];
[filterGroup addFilter:filter4];
[filter1 addTarget:filter2];
[filter2 addTarget:filter3];
[filter3 addTarget:filter4];
filterGroup.initialFilters=@[filter1];
filterGroup.terminalFilter=filter4;
[filterGroup forceProcessingAtSize:image.size];
[pic removeAllTargets];
[pic addTarget:filterGroup];
[pic processImage];
[filterGroup useNextFrameForImageCapture];
return [filterGroup imageFromCurrentFramebuffer];
}
filterGroup的很多操作都是利用最后一个Filter实现的,例如:
//GPUImageFilterGroup.m文件
- (void)useNextFrameForImageCapture;
{
[self.terminalFilter useNextFrameForImageCapture];
}
- (CGImageRef)newCGImageFromCurrentlyProcessedOutput;
{
return [self.terminalFilter newCGImageFromCurrentlyProcessedOutput];
}
GPUImageFilterPipeline用法
+ (UIImage *)applyPipelineFilter:(UIImage *)image
{
GPUImagePicture *pic=[[GPUImagePicture alloc]initWithImage:image];
//RGB滤镜
GPUImageRGBFilter * RGBFilter = [[GPUImageRGBFilter alloc] init];
//卡通滤镜
GPUImageToonFilter * toonFilter = [[GPUImageToonFilter alloc] init];
NSMutableArray *arrayTemp=[NSMutableArray array];
[arrayTemp addObject:RGBFilter];
[arrayTemp addObject:toonFilter];
GPUImageFilterPipeline * filterPipeline = [[GPUImageFilterPipeline alloc] initWithOrderedFilters:arrayTemp input:pic output:[[GPUImageView alloc]init]];
[pic processImage];
[toonFilter useNextFrameForImageCapture];//这个filter是toonFilter
return [filterPipeline currentFilteredFrame];
}
Pieline内部也把Filter串连了起来:
//GPUImageFilterPipeline.m文件
- (void)_refreshFilters {
id prevFilter = self.input;
GPUImageOutput<GPUImageInput> *theFilter = nil;
for (int i = 0; i < [self.filters count]; i++) {
theFilter = [self.filters objectAtIndex:i];
[prevFilter removeAllTargets];
[prevFilter addTarget:theFilter];
prevFilter = theFilter;
}
[prevFilter removeAllTargets];
//output为显示视图,可使用GPUImageView,也可为nil
if (self.output != nil) {
[prevFilter addTarget:self.output];
}
}
混合滤镜原理
模仿group与pipeline,快速实现混合滤镜:
+ (UIImage *)applyChainFilter:(UIImage *)image
{
GPUImagePicture *pic=[[GPUImagePicture alloc]initWithImage:image];
GPUImageRGBFilter *filter1 = [[GPUImageRGBFilter alloc] init];
GPUImageToonFilter *filter2 = [[GPUImageToonFilter alloc] init];
GPUImageColorInvertFilter *filter3 = [[GPUImageColorInvertFilter alloc] init];
//串连起来
[filter1 addTarget:filter2];
[filter2 addTarget:filter3];
[pic removeAllTargets];
[pic addTarget:filter1];
[filter1 forceProcessingAtSize:image.size];
[filter2 forceProcessingAtSize:image.size];
[filter3 forceProcessingAtSize:image.size];
//处理得到图片
[pic processImage];
[filter3 useNextFrameForImageCapture];
return [filter3 imageFromCurrentFramebuffer];
}