GPUImage学习日记(1)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/churk123/article/details/77511723

简介: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(@"保存成功");
    }
}
运行结果


展开阅读全文

没有更多推荐了,返回首页