前言
本片為GPUImage系列的第二篇喻圃,混合濾鏡萤彩,本篇文章將從兩種方式講解GPUImage混合濾鏡的使用粪滤。
本文系列第一篇:初見(jiàn)篇已經(jīng)完結(jié)斧拍,感興趣可以看看:
iOS開(kāi)發(fā)之GPUImage(一)—初探(給相機(jī)加濾鏡)
正文
GPUImage混合濾鏡顧名思義即多種濾鏡效果組合使用。就像我們?yōu)殓R頭加一個(gè)單獨(dú)的光亮效果或者懷舊效果杖小,如果想要同時(shí)為鏡頭添加上述兩種或者多種濾鏡肆汹,我們就要用到組合濾鏡了。
GPUImage所提供的混合濾鏡有兩種方式:
- GPUImageFilterGroup
- GPUImageFilterPipeline
使用GPUImageFilterGroup混合濾鏡的步驟如下:
1)初始化要加載濾鏡的GPUImagePicture對(duì)象initWithImage: smoothlyScaleOutput:
2)初始化多個(gè)要被使用的單獨(dú)的GPUImageFilter濾鏡
3)初始化GPUImageFilterGroup對(duì)象
4)將FilterGroup加在之前初始化過(guò)的GPUImagePicture上
5)將多個(gè)濾鏡加在FilterGroup中(此處切記一定要設(shè)置好設(shè)置FilterGroup的初始濾鏡和末尾濾鏡)
6)之前初始化過(guò)的GPUImagePicture處理圖片 processImage
7)拿到處理后的UIImage對(duì)象圖片imageFromCurrentFramebuffer
廢話少說(shuō)上代碼:
- (void)viewDidLoad {
[super viewDidLoad];
//加載一個(gè)UIImage對(duì)象
UIImage *image = [UIImage imageNamed:@"image.jpg"];
//初始化GPUImagePicture
_picture = [[GPUImagePicture alloc] initWithImage:image smoothlyScaleOutput:YES];
//反色濾鏡
GPUImageColorInvertFilter *invertFilter = [[GPUImageColorInvertFilter alloc] init];
//伽馬線濾鏡
GPUImageGammaFilter *gammaFilter = [[GPUImageGammaFilter alloc]init];
gammaFilter.gamma = 0.2;
//曝光度濾鏡
GPUImageExposureFilter *exposureFilter = [[GPUImageExposureFilter alloc]init];
exposureFilter.exposure = -1.0;
//懷舊
GPUImageSepiaFilter *sepiaFilter = [[GPUImageSepiaFilter alloc] init];
/*
*FilterGroup的方式混合濾鏡
*/
//初始化GPUImageFilterGroup
self.myFilterGroup = [[GPUImageFilterGroup alloc] init];
//將濾鏡組加在GPUImagePicture上
[_picture addTarget:self.myFilterGroup];
//將濾鏡加在FilterGroup中
[self addGPUImageFilter:invertFilter];
[self addGPUImageFilter:gammaFilter];
[self addGPUImageFilter:exposureFilter];
[self addGPUImageFilter:sepiaFilter];
//處理圖片
[_picture processImage];
[self.myFilterGroup useNextFrameForImageCapture];
//拿到處理后的圖片
UIImage *dealedImage = [self.myFilterGroup imageFromCurrentFramebuffer];
self.myImageView.image = dealedImage;
}
#pragma mark 將濾鏡加在FilterGroup中并且設(shè)置初始濾鏡和末尾濾鏡
- (void)addGPUImageFilter:(GPUImageFilter *)filter{
[self.myFilterGroup addFilter:filter];
GPUImageOutput<GPUImageInput> *newTerminalFilter = filter;
NSInteger count = self.myFilterGroup.filterCount;
if (count == 1)
{
//設(shè)置初始濾鏡
self.myFilterGroup.initialFilters = @[newTerminalFilter];
//設(shè)置末尾濾鏡
self.myFilterGroup.terminalFilter = newTerminalFilter;
} else
{
GPUImageOutput<GPUImageInput> *terminalFilter = self.myFilterGroup.terminalFilter;
NSArray *initialFilters = self.myFilterGroup.initialFilters;
[terminalFilter addTarget:newTerminalFilter];
//設(shè)置初始濾鏡
self.myFilterGroup.initialFilters = @[initialFilters[0]];
//設(shè)置末尾濾鏡
self.myFilterGroup.terminalFilter = newTerminalFilter;
}
}
使用GPUImageFilterPipeline混合濾鏡的步驟如下:
1)初始化要加載濾鏡的GPUImagePicture對(duì)象initWithImage: smoothlyScaleOutput:
2)初始化GPUImageView并加在自己的UIImageView對(duì)象上
3)初始化多個(gè)要被使用的單獨(dú)的GPUImageFilter濾鏡
4)把多個(gè)單獨(dú)的濾鏡對(duì)象放到數(shù)組中
5)初始化創(chuàng)建GPUImageFilterPipeline對(duì)象initWithOrderedFilters: input: output:
參數(shù)1:濾鏡數(shù)組
參數(shù)2:GPUImagePicture對(duì)象
參數(shù)3:GPUImageOutput對(duì)象如:GPUImageView
6)之前初始化過(guò)的GPUImagePicture處理圖片 processImage
7)拿到處理后的UIImage對(duì)象圖片currentFilteredFrame
廢話少說(shuō)上代碼:
- (void)viewDidLoad {
[super viewDidLoad];
//加載一個(gè)UIImage對(duì)象
UIImage *image = [UIImage imageNamed:@"image.jpg"];
//初始化GPUImagePicture
_picture = [[GPUImagePicture alloc] initWithImage:image smoothlyScaleOutput:YES];
//反色濾鏡
GPUImageColorInvertFilter *invertFilter = [[GPUImageColorInvertFilter alloc] init];
//伽馬線濾鏡
GPUImageGammaFilter *gammaFilter = [[GPUImageGammaFilter alloc]init];
gammaFilter.gamma = 0.2;
//曝光度濾鏡
GPUImageExposureFilter *exposureFilter = [[GPUImageExposureFilter alloc]init];
exposureFilter.exposure = -1.0;
//懷舊
GPUImageSepiaFilter *sepiaFilter = [[GPUImageSepiaFilter alloc] init];
/*
*GPUImageFilterPipeline的方式混合濾鏡
*/
//初始化myGpuImageView
_myGpuImageView = [[GPUImageView alloc] initWithFrame:self.myImageView.bounds];
[self.myImageView addSubview:self.myGpuImageView];
//把多個(gè)濾鏡對(duì)象放到數(shù)組中
NSMutableArray *filterArr = [NSMutableArray array];
[filterArr addObject:invertFilter];
[filterArr addObject:gammaFilter];
[filterArr addObject:exposureFilter];
[filterArr addObject:sepiaFilter];
//創(chuàng)建GPUImageFilterPipeline對(duì)象
GPUImageFilterPipeline *filterPipline = [[GPUImageFilterPipeline alloc] initWithOrderedFilters:filterArr input:_picture output:self.myGpuImageView];
//處理圖片
[_picture processImage];
[sepiaFilter useNextFrameForImageCapture];
//拿到處理后的圖片
UIImage *dealedImage = [filterPipline currentFilteredFrame];
效果圖如下:
源碼已上傳至fenglinyunshi-git予权,歡迎下載昂勉,并提出寶貴意見(jiàn)。
結(jié)語(yǔ)
本篇主要講解了GPUImage框架混合濾鏡的兩種用法扫腺,同樣的做法同樣可以運(yùn)用到給相機(jī)加濾鏡的應(yīng)用場(chǎng)景中岗照。
未完待續(xù)...
博觀而約取,厚積而薄發(fā)。