示例:使用GPUImageFilterGroup为GPUImagePicture添加组合滤镜
代码:
//
// ViewController.m
// GPUImageTest
//
// Created by RoomBlin 16/7/1.
// Copyright © 2017年 Blin. All rights reserved.
//
#import "ViewController.h"
#import "GPUImage.h"
@interface ViewController ()
{
UIImage *_inputImage;
UIImage *_outputImage;
}
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (nonatomic,strong) GPUImagePicture *picture;
@property (nonatomic,strong) GPUImageView *imageView;
@property (nonatomic,strong) GPUImageFilterGroup *filterGroup;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 图片输入源
_inputImage = [UIImage imageNamed:@"icon"];
// 初始化 picture
_picture = [[GPUImagePicture alloc] initWithImage:_inputImage smoothlyScaleOutput:YES];
// 初始化 imageView
_imageView = [[GPUImageView alloc] initWithFrame:self.iconImageView.bounds];
[self.iconImageView addSubview:_imageView];
// 初始化 filterGroup
_filterGroup = [[GPUImageFilterGroup alloc] init];
[_picture addTarget:_filterGroup];
// 添加 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];
[self addGPUImageFilter:filter1];
[self addGPUImageFilter:filter2];
[self addGPUImageFilter:filter3];
[self addGPUImageFilter:filter4];
// 处理图片
[_picture processImage];
[_filterGroup useNextFrameForImageCapture];
self.iconImageView.image = [_filterGroup imageFromCurrentFramebuffer];
}
- (void)addGPUImageFilter:(GPUImageOutput<GPUImageInput> *)filter
{
[_filterGroup addFilter:filter];
GPUImageOutput<GPUImageInput> *newTerminalFilter = filter;
NSInteger count = _filterGroup.filterCount;
if (count == 1)
{
_filterGroup.initialFilters = @[newTerminalFilter];
_filterGroup.terminalFilter = newTerminalFilter;
} else
{
GPUImageOutput<GPUImageInput> *terminalFilter = _filterGroup.terminalFilter;
NSArray *initialFilters = _filterGroup.initialFilters;
[terminalFilter addTarget:newTerminalFilter];
_filterGroup.initialFilters = @[initialFilters[0]];
_filterGroup.terminalFilter = newTerminalFilter;
}
}
@end
![组合滤镜效果.png](https://img.haomeiwen.com/i1252318/09774df7dad3664f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
实时滤镜效果的实现,跟上边的差不多
//
// ViewController.m
// GPUImageTest
//
// Created by 黄健 on 16/7/1.
// Copyright © 2016年 黄健. All rights reserved.
//
#import "ViewController.h"
#import "GPUImage.h"
@interface ViewController ()
@property (nonatomic,strong) GPUImageVideoCamera *videoCamera;
@property (nonatomic,strong) GPUImageView *filterImageView;
@property (nonatomic,strong) GPUImageFilterGroup *filterGroup;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 初始化 videoCamera
_videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPresetHigh cameraPosition:AVCaptureDevicePositionBack];
_videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
_videoCamera.horizontallyMirrorFrontFacingCamera = NO;
_videoCamera.horizontallyMirrorRearFacingCamera = NO;
// 初始化 filterGroup
_filterGroup = [[GPUImageFilterGroup alloc] init];
[_videoCamera addTarget:_filterGroup];
// 添加 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];
GPUImageSepiaFilter *filter3 = [[GPUImageSepiaFilter alloc] init];
GPUImageColorInvertFilter *filter4 = [[GPUImageColorInvertFilter alloc] init];
[self addGPUImageFilter:filter1];
[self addGPUImageFilter:filter2];
[self addGPUImageFilter:filter3];
[self addGPUImageFilter:filter4];
// 初始化 imageView
_filterImageView = [[GPUImageView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:_filterImageView];
[_filterGroup addTarget:_filterImageView];
[_videoCamera startCameraCapture];
}
- (void)addGPUImageFilter:(GPUImageOutput<GPUImageInput> *)filter
{
[_filterGroup addFilter:filter];
GPUImageOutput<GPUImageInput> *newTerminalFilter = filter;
NSInteger count = _filterGroup.filterCount;
if (count == 1)
{
_filterGroup.initialFilters = @[newTerminalFilter];
_filterGroup.terminalFilter = newTerminalFilter;
} else
{
GPUImageOutput<GPUImageInput> *terminalFilter = _filterGroup.terminalFilter;
NSArray *initialFilters = _filterGroup.initialFilters;
[terminalFilter addTarget:newTerminalFilter];
_filterGroup.initialFilters = @[initialFilters[0]];
_filterGroup.terminalFilter = newTerminalFilter;
}
}
网友评论