内容简介:之前我的一篇笔记还是使用之前提到的根据以上代码,
之前我的一篇笔记 iOS实时美颜并获取原图 里面写了如何实现实时的美颜滤镜,本篇作为补充说下如何给一张图片美颜。
还是使用之前提到的 GPUImageBeautifyFilter
// 原图
UIImage * sourceImage = [UIImage imageNamed:@"notBeauty.png"];
GPUImageBeautifyFilter *beautyFilter = [[GPUImageBeautifyFilter alloc] init];
//设置要渲染的区域
[beautyFilter forceProcessingAtSize:sourceImage.size];
[beautyFilter useNextFrameForImageCapture];
//获取数据源
GPUImagePicture *stillImageSource = [[GPUImagePicture alloc]initWithImage:sourceImage];
//添加上滤镜
[stillImageSource addTarget:beautyFilter];
//开始渲染
[stillImageSource processImage];
//获取滤镜后的图片
UIImage *beautyImage = [beautyFilter imageFromCurrentFramebuffer];
[beautyFilter removeAllTargets];
[stillImageSource removeAllTargets];
根据以上代码, UIImage *beautyImage
存储的就是美颜后的图片。
上面写的是如何把一个图片美颜,下面说下 如何使用GPUImage实现一个美颜相机 。
看如下示例代码
- (void)viewDidLoad {
[super viewDidLoad];
CGRect mainScreenFrame = [[UIScreen mainScreen] bounds];
GPUImageView *primaryView = [[GPUImageView alloc] initWithFrame:mainScreenFrame];
primaryView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.takePhotoButton = [[UIButton alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2 - 25, [UIScreen mainScreen].bounds.size.height - 60, 50, 50)];
[self.takePhotoButton setTitle:@"拍照" forState:UIControlStateNormal];
self.takePhotoButton.backgroundColor = [UIColor clearColor];
[self.takePhotoButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.takePhotoButton addTarget:self action:@selector(takePhotoButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[primaryView addSubview:self.takePhotoButton];
self.view = primaryView;
self.cameraPosition = AVCaptureDevicePositionBack;
[self initCameraAndFilterWithPosition:self.cameraPosition];
}
// 初始化美颜相机
- (void)initCameraAndFilterWithPosition:(AVCaptureDevicePosition)position {
self.stillCamera = [[GPUImageStillCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:position];
self.stillCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
if (AVCaptureDevicePositionFront == position) {
// 前置摄像头,镜像展示
self.stillCamera.horizontallyMirrorFrontFacingCamera = YES;
}
self.beautyFilter = [[GPUImageBeautifyFilter alloc] init];
[self.stillCamera addTarget:self.beautyFilter];
GPUImageView * filterView = (GPUImageView *)self.view;
[self.beautyFilter addTarget:filterView];
[self.stillCamera startCameraCapture];
}
// 点击了拍照按钮
- (void)takePhotoButtonClicked {
[self.stillCamera capturePhotoAsJPEGProcessedUpToFilter:self.beautyFilter withCompletionHandler:^(NSData *processedJPEG, NSError *error){
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
// 获取美颜过的相片
UIImage * beautyPhoto = [UIImage imageWithData:processedJPEG];
// ……
// ……
});
}];
}
如果要获取未美颜的原图,可以参照我上一篇笔记 iOS实时美颜滤镜并获取原图
里面的方法原理获取原图。即实现 GPUImageVideoCameraDelegate
协议里面的方法 - (void)willOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
。
这里有个需要注意的点:使用GPUImage的前置摄像头与后置摄像头获取到的原图方向不是一样的,可能是因为前置摄像头的时候设置了 horizontallyMirrorFrontFacingCamera = YES
的原因。
leftMirror left
所以,如果使用后置摄像头拍照,获取未美颜的原图的时候,转换代码如下:
- (UIImage *)fixNotBeautyImageOrientation:(UIImage *)originImage {
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformRotate(transform, -M_PI_2);
transform = CGAffineTransformTranslate(transform, - originImage.size.width, 0);
CGContextRef ctx = CGBitmapContextCreate(NULL, originImage.size.height, originImage.size.width,
CGImageGetBitsPerComponent(originImage.CGImage), 0,
CGImageGetColorSpace(originImage.CGImage),
CGImageGetBitmapInfo(originImage.CGImage));
CGContextConcatCTM(ctx, transform);
CGContextDrawImage(ctx, CGRectMake(0,0, originImage.size.width, originImage.size.height), originImage.CGImage);
CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImage imageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
return img;
}
以上所述就是小编给大家介绍的《iOS给一张照片美颜》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 美颜重磅技术之 GPUImage 源码分析
- 直播SDK加入GPU自定义美颜
- iOS实现实时美颜滤镜并获取原图
- 美颜相机中的设计模式——装饰者模式
- 开发者回应 iPhone XS 美颜:没内置滤镜、算法可改进
- 算法美颜过的月亮还是真实的月亮吗?华为P月算法深陷舆论漩涡
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
MD5 加密
MD5 加密工具
html转js在线工具
html转js在线工具