GPUImage学习日记(1)

简介:GPUImage 是基于GPU处理图像的一个开源库,对图像处理有巨大优势。

引用方式:可直接把源码拖入项目也可直接引用静态库文件。
通过github官方介绍,简单写了个demo。大致思路如下
  • 给GPUImageStillCamera添加滤镜GPUImageSketchFilter
  • 给filter添加输出imageView 即GPUImageView
  • 最后将输出界面添加到视图即可
代码如下
@interface ViewController ()
@property(strong, nonatomic) GPUImageStillCamera *videoCamera;
@property(strong, nonatomic) GPUImageFilter *customFilter;
@property(strong, nonatomic) GPUImageView *filteredVideoView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _videoCamera = [[GPUImageStillCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack];
    _videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;

    _customFilter = [[GPUImageSketchFilter alloc] init];
    _filteredVideoView = [[GPUImageView alloc] initWithFrame:self.view.bounds];
    // Add the view somewhere so it's visible
    [_videoCamera addTarget:_customFilter];
    [_customFilter addTarget:_filteredVideoView];
    [self.view addSubview:_filteredVideoView];
    [_videoCamera startCameraCapture];



    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake((self.view.bounds.size.width-50)*0.5, self.view.bounds.size.height-60, 50, 50)];
    btn.layer.cornerRadius = 25;
    btn.backgroundColor = [UIColor redColor];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(takePhoto) forControlEvents:UIControlEventTouchUpInside];


}

-(void)takePhoto{

    [_videoCamera capturePhotoAsImageProcessedUpToFilter:_customFilter withCompletionHandler:^(UIImage *processedImage, NSError *error) {

        UIImageWriteToSavedPhotosAlbum(processedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    }];
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    if (error) {
        NSLog(@"保存失败");
    } else {
        NSLog(@"保存成功");
    }
}
运行结果

GPUImage学习日记(1)_第1张图片GPUImage学习日记(1)_第2张图片

你可能感兴趣的:(IOS随笔札记)