GPUImage的基础应用(二):动态影像加滤镜

听名字很高大上,其实就是我们平常用到的:打开相机录制视频的时候添加上滤镜,实现美化的功能。

录制视频添加滤镜
    //参数1 相机捕获数据源的质量
    //参数2 摄像头的位置
    GPUImageVideoCamera *videoCamera = [[GPUImageVideoCamera alloc]initWithSessionPreset:AVCaptureSessionPresetHigh cameraPosition:AVCaptureDevicePositionFront];
    
    self.videoCamera = videoCamera;
    
    //捕获视频或图像的时候是否跟着设备旋转
    videoCamera.horizontallyMirrorRearFacingCamera = NO;
    videoCamera.horizontallyMirrorFrontFacingCamera = NO;
    
    //捕获画面的方向
    videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
    
    //创建滤镜
    GPUImageSepiaFilter *filter = [[GPUImageSepiaFilter alloc]init];
//    GPUImageSwirlFilter *filter = [[GPUImageSwirlFilter alloc]init];
    
    //相机添加滤镜对象
    [videoCamera addTarget:filter];
    
    //创建视图,显示滤镜捕获资源
    GPUImageView *imageView = [[GPUImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:imageView];
    
    //滤镜添加视图
    [filter addTarget:imageView];
    
    //相机启动
    [videoCamera startCameraCapture];
播放录像添加滤镜
-(void)playMovieAddFilter{//播放录像滤镜
    
    NSString *path = [[NSBundle mainBundle]pathForResource:@"video" ofType:@"mp4"];
    NSURL *url = [NSURL fileURLWithPath:path];
    
    GPUImageMovie *movie = [[GPUImageMovie alloc]initWithURL:url];
//    self.movie = movie;
    movie.shouldRepeat = YES;
    movie.playAtActualSpeed = YES;
//    movie.delegate = self;
    
    GPUImagePixellateFilter *filter = [[GPUImagePixellateFilter alloc]init];
    
    [movie addTarget:filter];
    
    GPUImageView *imageView = [[GPUImageView alloc]initWithFrame:self.view.frame];
    [filter addTarget:imageView];
    [self.view addSubview:imageView];
    [movie startProcessing];
}

你可能感兴趣的:(GPUImage的基础应用(二):动态影像加滤镜)