示例:使用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) 濾鏡組添加每個(gè)濾鏡
2. 按添加順序(可自行調(diào)整)前一個(gè)filter(addTarget) 添加后一個(gè)filter
3. filterGroup.initialFilters = @[第一個(gè)filter]];
4. filterGroup.terminalFilter = 最后一個(gè)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
實(shí)時(shí)濾鏡效果的實(shí)現(xiàn)逗宁,跟上邊的差不多
//
// 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) 濾鏡組添加每個(gè)濾鏡
2. 按添加順序(可自行調(diào)整)前一個(gè)filter(addTarget) 添加后一個(gè)filter
3. filterGroup.initialFilters = @[第一個(gè)filter]];
4. filterGroup.terminalFilter = 最后一個(gè)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;
}
}