前言
- 一些自带滤镜
//磨皮GPUImageBilateralFilter
//绿巨人GPUImageHueFilter
//移行幻影GPUImageLowPassFilter
//怀旧GPUImageMonochromeFilter、GPUImageSepiaFilter
//黑白GPUImageOpeningFilter
//像素GPUImagePixellateFilter
//油画GPUImagePosterizeFilter、GPUImageSmoothToonFilter
//素描GPUImageSketchFilter、GPUImageThresholdSketchFilter
//--------------GPUImageSkinToneFilter
//血轮眼GPUImageSwirlFilter
- 支持滤镜摄像,滤镜相机,滤镜图片,滤镜视频
// - GPUImageVideoCamera (for live video from an iOS camera)
// - GPUImageStillCamera (for taking photos with the camera)
// - GPUImagePicture (for still images)
// - GPUImageMovie (for movies)
一.使用滤镜生成UIImage对象
- 创建一个滤镜类
- 使用GPUImagePicture加载滤镜
- processImage呈现图像
- useNextFrameForImageCapture持续图片呈现(必须要加这句不然会生成nil)
- imageFromCurrentFramebuffer生成图片
+ (UIImage *)applySoftEleganceFilter:(UIImage *)image
{
GPUImageSoftEleganceFilter *filter = [[GPUImageSoftEleganceFilter alloc] init];
[filter forceProcessingAtSize:CGSizeMake(image.size.width / 2.0, image.size.height / 2.0)];
GPUImagePicture *pic = [[GPUImagePicture alloc] initWithImage:image];
[pic addTarget:filter];
[pic processImage];
[filter useNextFrameForImageCapture];
return [filter imageFromCurrentFramebuffer];
}
二.使用滤镜相机
- 定义相机的输出页面
GPUImageView *cameraView = [[GPUImageView alloc] initWithFrame:CGRectMake(0, 44,GBWidth, GBWidth + 145 * GBScale - 44)];
//设置为水平镜像,因为磨人是前置,会有镜像
[cameraView setInputRotation:kGPUImageFlipHorizonal atIndex:0];
cameraView.fillMode = kGPUImageFillModePreserveAspectRatioAndFill;
[self.view addSubview:cameraView];
self.cameraView = cameraView;
- 定义滤镜操作对象
stillCamera = [[GPUImageStillCamera alloc] initWithSessionPreset:@"AVCaptureSessionPresetPhoto" cameraPosition:AVCaptureDevicePositionFront];
stillCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
- 定义滤镜需要的对象
//初始化自己需要的滤镜
filter = [[GPUImageBilateralFilter alloc] init];
- 添加滤镜
[stillCamera addTarget:filter];
- 将滤镜添加到相机
[filter addTarget:cameraView];
- 开启滤镜
[stillCamera startCameraCapture];
- 拍照
GPUImageStillCamera *stillCamera;
GPUImageOutput<GPUImageInput> *filter, *secondFilter, *terminalFilter;
GPUImagePicture *memoryPressurePicture1, *memoryPressurePicture2;
- (void)setupCameraView {
//定义相机输出界面
GPUImageView *cameraView = [[GPUImageView alloc] initWithFrame:CGRectMake(0, 44,GBWidth, GBWidth + 145 * GBScale - 44)];
//设置为水平镜像,因为磨人是前置,会有镜像
[cameraView setInputRotation:kGPUImageFlipHorizonal atIndex:0];
cameraView.fillMode = kGPUImageFillModePreserveAspectRatioAndFill;
[self.view addSubview:cameraView];
self.cameraView = cameraView;
//定义滤镜操作对象
stillCamera = [[GPUImageStillCamera alloc] initWithSessionPreset:@"AVCaptureSessionPresetPhoto" cameraPosition:AVCaptureDevicePositionBack];
stillCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
//定义需要的滤镜
filter = [[GPUImagePixellateFilter alloc] init];
[stillCamera addTarget:filter];
//滤镜添加到相机中
[filter addTarget:cameraView];
//开启滤镜
[stillCamera startCameraCapture];
[self.cameraView setInputRotation:kGPUImageFlipHorizonal atIndex:0];
}
三.使用滤镜录像
1.创建录像机对象GPUImageVideoCamera
2.创建滤镜对象
3.创建视频的输出页面
4.创建视频写入GPUImageMovieWriter
5.添加视频写入添加滤镜
6.开始录像
7.保存
GPUImageVideoCamera *videoCamera;
GPUImageOutput<GPUImageInput> *filter;
GPUImageMovieWriter *movieWriter;
videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack];
videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
videoCamera.horizontallyMirrorFrontFacingCamera = NO;
videoCamera.horizontallyMirrorRearFacingCamera = NO;
filter = [[GPUImageSepiaFilter alloc] init];
[videoCamera addTarget:filter];
GPUImageView *filterView = [[GPUImageView alloc] initWithFrame:CGRectMake(0, 44,GBWidth, GBWidth + 145 * GBScale - 44)];
filterView.fillMode = kGPUImageFillModePreserveAspectRatioAndFill;
[self.view addSubview:filterView];
NSString *pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Movie.m4v"];
unlink([pathToMovie UTF8String]); // If a file already exists, AVAssetWriter won't let you record new frames, so delete the old movie
NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie];
movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(480.0, 640.0)];
movieWriter.encodingLiveVideo = YES;
[filter addTarget:movieWriter];
[filter addTarget:filterView];
[videoCamera startCameraCapture];
double delayToStartRecording = 0.5;
dispatch_time_t startTime = dispatch_time(DISPATCH_TIME_NOW, delayToStartRecording * NSEC_PER_SEC);
dispatch_after(startTime, dispatch_get_main_queue(), ^(void){
NSLog(@"Start recording");
videoCamera.audioEncodingTarget = movieWriter;
[movieWriter startRecording];
double delayInSeconds = 10.0;
dispatch_time_t stopTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(stopTime, dispatch_get_main_queue(), ^(void){
[filter removeTarget:movieWriter];
videoCamera.audioEncodingTarget = nil;
[movieWriter finishRecording];
NSLog(@"Movie completed");
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:movieURL])
{
[library writeVideoAtPathToSavedPhotosAlbum:movieURL completionBlock:^(NSURL *assetURL, NSError *error)
{
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Video Saving Failed"
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Video Saved" message:@"Saved To Photo Album"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
});
}];
}
// [videoCamera.inputCamera lockForConfiguration:nil];
// [videoCamera.inputCamera setTorchMode:AVCaptureTorchModeOff];
// [videoCamera.inputCamera unlockForConfiguration];
});
});
Demo下载链接:https://pan.baidu.com/s/1miKwbfq