iOS GPUImage之GPUImageFilterGroup组合滤镜(5)

【提醒】 添加滤镜的顺序不同,效果也不同!


示例:使用GPUImageFilterGroup为GPUImagePicture添加组合滤镜

代码:


//
//  ViewController.m
//  GPUImageTest
//
//  Created by 黄健 on 16/7/1.
//  Copyright © 2016年 黄健. 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 *)filter
{
    [_filterGroup addFilter:filter];

    GPUImageOutput *newTerminalFilter = filter;

    NSInteger count = _filterGroup.filterCount;

    if (count == 1)
    {
        _filterGroup.initialFilters = @[newTerminalFilter];
        _filterGroup.terminalFilter = newTerminalFilter;

    } else
    {
        GPUImageOutput *terminalFilter    = _filterGroup.terminalFilter;
        NSArray *initialFilters                          = _filterGroup.initialFilters;

        [terminalFilter addTarget:newTerminalFilter];

        _filterGroup.initialFilters = @[initialFilters[0]];
        _filterGroup.terminalFilter = newTerminalFilter;
    }
}

@end

》》运行

iOS GPUImage之GPUImageFilterGroup组合滤镜(5)_第1张图片


示例:使用GPUImageFilterGroup为GPUImageVideoCamera添加四种组合滤镜

//
//  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 *)filter
{
    [_filterGroup addFilter:filter];

    GPUImageOutput *newTerminalFilter = filter;

    NSInteger count = _filterGroup.filterCount;

    if (count == 1)
    {
        _filterGroup.initialFilters = @[newTerminalFilter];
        _filterGroup.terminalFilter = newTerminalFilter;

    } else
    {
        GPUImageOutput *terminalFilter    = _filterGroup.terminalFilter;
        NSArray *initialFilters                          = _filterGroup.initialFilters;

        [terminalFilter addTarget:newTerminalFilter];

        _filterGroup.initialFilters = @[initialFilters[0]];
        _filterGroup.terminalFilter = newTerminalFilter;
    }
}

@end

》》运行

iOS GPUImage之GPUImageFilterGroup组合滤镜(5)_第2张图片

参考文章
https://github.com/BradLarson/GPUImage/issues/112

你可能感兴趣的:(iOS GPUImage之GPUImageFilterGroup组合滤镜(5))