iOS GPUImage的介绍与GPUImagePicture(1)


GPUImage介绍

GPUImage是一个基于GPU图像和视频处理的开源iOS框架, 
提供各种各样的图像处理滤镜,并且支持照相机和摄像机的实时滤镜;

使用GPUImage处理图片比Core Image更简单,
只需要将过滤器赋给图片对象即可,不用考虑context或者设备等其他问题。 
但目前在iOS上能用的就只有高斯模糊,
而GPUImage可用的有FastBlur, GaussianBlur, GaussianSelectiveBlur 和 BoxBlur。
此外,作为开源框架的GPUImage还支持自定义的过滤器。

测试环境:GPUImage版本 0.1.7,Xcode 7.3,iOS 9.3

项目地址:https://github.com/BradLarson/GPUImage
推荐使用CocoaPods

GPUImagePicture 
处理单张静态图片,类似于用美图秀秀打开一张手机照片进行P图

示例:为图片添加褐色滤镜,并保存

代码:

//
//  ViewController.m
//  GPUImageDemo
//
//  Created by 黄健 on 16/6/30.
//  Copyright © 2016年 hj. All rights reserved.
//

#import "ViewController.h"

#import "GPUImage.h"

@interface ViewController ()
{
    UIImage *_inputImage;
    UIImage *_outputImage;
}

@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;

@property (nonatomic,strong) GPUImageOutput   *filter;
@property (nonatomic,strong) GPUImageView                    *filterImageView;
@property (nonatomic,strong) GPUImagePicture *stillImageSource;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 图片输入源
    _inputImage = [UIImage imageNamed:@"icon"];

    // 初始化图片
     self.stillImageSource = [[GPUImagePicture alloc] initWithImage:_inputImage];

    // 初始化滤镜
     self.filter = [[GPUImageSepiaFilter alloc] init];

    // 向图片添加滤镜
    [self.stillImageSource addTarget:_filter];

    // 开始处理图片
    [self.filter useNextFrameForImageCapture];
    [self.stillImageSource processImage];

    // 输出处理后的图片
    _outputImage = [_filter imageFromCurrentFramebuffer];

    // 显示处理后的图片
    self.iconImageView.image = _outputImage;
}

- (IBAction)saveToAblum:(id)sender
{
    // 保存图片到本地
    UIImageWriteToSavedPhotosAlbum(_outputImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{

    if (error == nil) {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已存入手机相册" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];

    }else{

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"保存失败" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
    }

}

- (IBAction)showInputImage:(id)sender
{
    // 显示原图
    self.iconImageView.image = _inputImage;
}

- (IBAction)showOutImage:(id)sender
{
    // 显示新图
    self.iconImageView.image = _outputImage;
}

- (IBAction)saveToTemp:(id)sender
{
    // 保存图片到沙盒

    NSString *sandBoxPath = NSHomeDirectory();

    NSString *saveImagePath = [sandBoxPath stringByAppendingString:@"/Documents/helloworld.png"];

    [UIImagePNGRepresentation(_outputImage) writeToFile:saveImagePath atomically:YES];


#if 0

    //Document目录
    NSArray *dirArray1    = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDir = [documentDir objectAtIndex:0];

    //Library目录
    NSArray *dirArray3    = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libraryDir  = [libraryDir objectAtIndex:0];

    //Caches目录
    NSArray *dirArray3    = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachesDir   = [cacheDir objectAtIndex:0];
#endif
}


@end


测试:

iOS GPUImage的介绍与GPUImagePicture(1)_第1张图片

iOS GPUImage的介绍与GPUImagePicture(1)_第2张图片

你可能感兴趣的:(iOS GPUImage的介绍与GPUImagePicture(1))