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:
推荐使用CocoaPods
GPUImagePicture
处理单张静态图片,类似于用美图秀秀打开一张手机照片进行P图
示例:为图片添加褐色滤镜,并保存
代码:
#import "ViewController.h"
#import "GPUImage.h"
@interface ViewController ()
{
UIImage *_inputImage;
UIImage *_outputImage;
}
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (nonatomic,strong) GPUImageOutput<GPUImageInput> *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
NSArray *dirArray1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = [documentDir objectAtIndex:0];
NSArray *dirArray3 = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryDir = [libraryDir objectAtIndex:0];
NSArray *dirArray3 = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDir = [cacheDir objectAtIndex:0];
#endif
}
@end
测试:

