iOS给一张照片美颜

栏目: IOS · 发布时间: 6年前

内容简介:之前我的一篇笔记还是使用之前提到的根据以上代码,

之前我的一篇笔记 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给一张照片美颜》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

面向对象分析与设计

面向对象分析与设计

Grady Booch、Robert A. Maksimchuk、Michael W. Engel、Bobbi J. Young、Jim Conallen、Kelli A. Houston / 王海鹏、潘加宇 / 人民邮电出版社 / 2009-8 / 79.00元

《面向对象分析与设计(第3版)》是UML创始人Grady Booch的代表作之一,书中介绍的概念都基于牢固的理论基础。同时,《面向对象分析与设计(第3版)》又是一本注重实效的书,面向架构师和软件开发者等软件工程实践者的实际需要。《面向对象分析与设计(第3版)》通过大量例子说明了基本概念,解释了方法,并展示了在不同领域的成功应用。全书分为理论和应用两部分。理论部分深刻剖析了面向对象分析与设计(OOA......一起来看看 《面向对象分析与设计》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具