ios 滤镜处理(详细滤镜介绍)及处理方法

版权声明:转载请标注原文地址。邮箱Quinn_F@126.com https://blog.csdn.net/Xoxo_x/article/details/52523912

**
首先你需要导入 CoreImage.framework 框架
然后我们先来看看3个主要的类:
CIContext:它与Core Graphics 和 OpenGL context类似,所有Core Image的处理流程都通过它来进行;
CIImage:它用来存放图片数据,可以通过UIImage,图片文件或像素数据创建;
CIFilter:通过它来定义过滤器的详细属性。
**

CIContext有两种初始化方法,分别对应GPU和CPU

 // 创建基于GPU的CIContext对象
 context = [CIContext contextWithEAGLContext: options:];

 // 创建基于CPU的CIContext对象
 //context = [CIContext contextWithOptions: [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES]
 forKey:kCIContextUseSoftwareRenderer]];

    一般采用第一种基于GPU的,因为效率要比CPU高很多,但是要注意的是基于GPU的CIContext对象无法跨应用访问。比如你打开UIImagePickerController要选张照片进行美化,如果你直接在UIImagePickerControllerDelegate的委托方法里调用CIContext对象进行处理,那么系统会自动将其降为基于CPU的,速度会变慢,所以正确的方法应该是在委托方法里先把照片保存下来,回到主类里再来处理
// 通过图片路径创建CIImage
 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"image" ofType:@"png"];
 NSURL *fileNameAndPath = [NSURL fileURLWithPath:filePath];
 beginImage = [CIImage imageWithContentsOfURL:fileNameAndPath];

 过滤周期就完成了,简单来说分以下几个步骤:
 1 初始化CIContext,CIImage
 2 初始化CIFilter并设置参数
 3 得到输出的图片
 4 将图片转化成能显示的UIImage类型
 如果想一张图片有多种过滤效果就需要重复23两步,并且要将上一个过滤器输出的图片作为下一个过滤器的参数

滤镜信息介绍

按效果分类:
 kCICategoryDistortionEffect 扭曲效果,比如bump、旋转、hole
 kCICategoryGeometryAdjustment 几何开着调整,比如仿射变换、平切、透视转换
 kCICategoryCompositeOperation 合并,比如源覆盖(source over)、最小化、源在顶(source atop)、色彩混合模式
 kCICategoryHalftoneEffect Halftone效果,比如screen、line screen、hatched
 kCICategoryColorAdjustment 色彩调整,比如伽马调整、白点调整、曝光
 kCICategoryColorEffect 色彩效果,比如色调调整、posterize
 kCICategoryTransition 图像间转换,比如dissolve、disintegrate with mask、swipe
 kCICategoryTileEffect 瓦片效果,比如parallelogram、triangle
 kCICategoryGenerator 图像生成器,比如stripes、constant color、checkerboard
 kCICategoryGradient 渐变,比如轴向渐变、仿射渐变、高斯渐变
 kCICategoryStylize 风格化,比如像素化、水晶化
 kCICategorySharpen 锐化、发光
 kCICategoryBlur 模糊,比如高斯模糊、焦点模糊、运动模糊
 按使用场景分类:
 kCICategoryStillImage 能用于静态图像
 kCICategoryVideo 能用于视频
 kCICategoryInterlaced 能用于交错图像
 kCICategoryNonSquarePixels 能用于非矩形像素
 kCICategoryHighDynamicRange 能用于HDR
 */

效果图:
这里写图片描述这里写图片描述这里写图片描述

代码如下:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 通过UIImage对象创建CIImage
    UIImage *image   = [UIImage imageNamed:@"demo.jpg"];
    CIImage *ciimage = [[CIImage alloc] initWithImage:image];
    // 根据图片获取尺寸
    CGRect rect      = CGRectMake(0, 0, self.view.frame.size.width, image.size.height);

    // 初始化GPUView
    _gpuView = [[GPUView alloc] initWithFrame:rect];
    [self.view addSubview:_gpuView];
    [_gpuView drawCIImage:ciimage];

    // 初始化第一个CIFilter  通过它来定义过滤器的详细属性。

    // 第一行:指定使用哪一个过滤器,
    //通过[CIFilter filterNamesInCategory: kCICategoryBuiltIn]能得到所有过滤器的列表
    _ciFilter1 = [CIFilter filterWithName:@"CISepiaTone"];
    //第二行:指定需要处理的图片
    [_ciFilter1 setValue:ciimage forKey:kCIInputImageKey];
    // 第三行:指定过滤参数,每个过滤器的参数都不一样
    //可以在官方文档里搜索“Core Image Filter Reference”查看
    [_ciFilter1 setValue:@0 forKey:kCIInputIntensityKey];


    // 初始化第二个CIFilter
    _ciFilter2 = [CIFilter filterWithName:@"CIHueAdjust"];
    [_ciFilter2 setValue:[_ciFilter1 outputImage] forKeyPath:kCIInputImageKey];
    [_ciFilter2 setValue:@0.f forKeyPath:kCIInputAngleKey];

    // 初始化UISlider
    _slider1 = [[UISlider alloc] initWithFrame:CGRectMake(0, 50, 320, 20)];
    [self.view addSubview:_slider1];
    [_slider1 addTarget:self
                 action:@selector(event1:)
       forControlEvents:UIControlEventValueChanged];
    _slider1.minimumValue = 0;
    _slider1.maximumValue = 1;
    _slider1.value = 0.5f;

    _slider2 = [[UISlider alloc] initWithFrame:CGRectMake(0, 100, 320, 20)];
    [self.view addSubview:_slider2];
    [_slider2 addTarget:self
                 action:@selector(event2:)
       forControlEvents:UIControlEventValueChanged];
    _slider2.minimumValue = -3.14f;
    _slider2.maximumValue = +3.14f;
    _slider2.value = 0.f;}

- (void)event1:(UISlider *)slider
{
    [_ciFilter1 setValue:[NSNumber numberWithFloat:_slider1.value]
                  forKey:kCIInputIntensityKey];
    [_ciFilter2 setValue:[_ciFilter1 outputImage] forKeyPath:kCIInputImageKey];
    [_ciFilter2 setValue:[NSNumber numberWithFloat:_slider2.value] forKeyPath:kCIInputAngleKey];
    [_gpuView drawCIImage:[_ciFilter2 outputImage]];
}

- (void)event2:(UISlider *)slider
{
    [_ciFilter1 setValue:[NSNumber numberWithFloat:_slider1.value]
                  forKey:kCIInputIntensityKey];
    [_ciFilter2 setValue:[_ciFilter1 outputImage] forKeyPath:kCIInputImageKey];
    [_ciFilter2 setValue:[NSNumber numberWithFloat:_slider2.value] forKeyPath:kCIInputAngleKey];
    [_gpuView drawCIImage:[_ciFilter2 outputImage]];
}
//- (void)event:(UISlider *)slider
//{
//    [_ciFilter setValue:[NSNumber numberWithFloat:slider.value]
//                 forKey:kCIInputIntensityKey];
//    CIImage *outputImage = [_ciFilter outputImage];
//    [_gpuView drawCIImage:outputImage];
//}
//打印出所有可用滤镜信息
- (void)logAllFilters {
    NSArray *properties = [CIFilter filterNamesInCategory: kCICategoryBuiltIn];
    NSLog(@"%@", properties);
    for (NSString *filterName in properties)
    {
        CIFilter *fltr = [CIFilter filterWithName:filterName];
        NSLog(@"%@", [fltr attributes]);
    }
}

//
- (CIImage *)oldPhoto:(CIImage *)img withAmount:(float)intensity
{

    // 1
    CIFilter *sepia = [CIFilter filterWithName:@"CISepiaTone"];
    [sepia setValue:img forKey:kCIInputImageKey];
    [sepia setValue:@(intensity) forKey:@"inputIntensity"];
    // 2
    CIFilter *random = [CIFilter filterWithName:@"CIRandomGenerator"];
    // 3
    CIFilter *lighten = [CIFilter filterWithName:@"CIColorControls"];
    [lighten setValue:random.outputImage forKey:kCIInputImageKey];
    [lighten setValue:@(1 - intensity) forKey:@"inputBrightness"];
    [lighten setValue:@0.0 forKey:@"inputSaturation"];
    // 4
    CIImage *croppedImage = [lighten outputImage] ;

    return croppedImage;
}
@end
#import "ViewController.h"

#import <CoreImage/CoreImage.h>
#import "GPUView.h"

@interface ViewController ()
@property (nonatomic, strong) CIFilter  *ciFilter1;
@property (nonatomic, strong) CIFilter  *ciFilter2;
@property (nonatomic, strong) GPUView   *gpuView;
@property (nonatomic, strong) UISlider  *slider1;
@property (nonatomic, strong) UISlider  *slider2;
@end
//
//  GPUView.m
//  OpenGL_ES_1
//
//  Created by fsk-0-1-n on 16/9/8.
//  Copyright © 2016年 Xoxo. All rights reserved.
//

#import "GPUView.h"

@interface GPUView ()

@property (nonatomic, assign)  CGRect     rectInPixels;
@property (nonatomic, strong)  CIContext *context;
@property (nonatomic, strong)  GLKView   *showView;

@end

@implementation GPUView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // 获取OpenGLES渲染环境
        EAGLContext *eaglContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

        // 初始化GLKView并指定OpenGLES渲染环境 + 绑定
        _showView = [[GLKView alloc] initWithFrame:frame context:eaglContext];
        [_showView bindDrawable];

        // 添加进图层
        [self addSubview:_showView];

        // 创建CIContext环境
        _context = \
        [CIContext contextWithEAGLContext:eaglContext
                                  options:@{kCIContextWorkingColorSpace:[NSNull null]}];

        // 定义绘制区域(像素描述)
        _rectInPixels = \
        CGRectMake(0.0, 0.0, _showView.drawableWidth, _showView.drawableHeight);
    }
    return self;
}
你也可以把图片导入到手机相册
- (void)drawCIImage:(CIImage *)ciImage
{
    // 开始绘制
    [_context drawImage:ciImage
                 inRect:_rectInPixels
               fromRect:[ciImage extent]];
    //将CIImage转变为UIImage
//    CGImageRef cgimg = [_context createCGImage:ciImage fromRect:[ciImage extent]];
//    UIImage *newImg = [UIImage imageWithCGImage:cgimg];
//    CGImageRelease(cgimg);
    // 显示
    [_showView display];
}

小弟最近正在研究语音识别,,,,,如有大神赐教,,万分感激,,,目前没有办法对人物的感情 进行识别,,如有人做过音乐软件,,,类似唱吧的应用,,希望提供思路。。

阅读更多
换一批

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