滤镜

       滤镜:主要是用来实现图像的各种特殊效果。

下面来介绍一下滤镜是怎样实现特殊效果。

一、滤镜的框架->CoreImage(是一个图像框架,不用手动导入,系统自带)

1、它基于OpenGL的顶层创建。

2、它利用CPU基于硬件加速来处理图像

3、CoreImage中有很多滤镜,它能够一次给予一张图像或者视频帧,多种视觉效果(即滤镜链)

4、滤镜链:滤镜可以连接起来组成一个滤镜链,可以把滤镜链的效果叠加起来处理图像。

二、使用到类

1、CIImage:保存图像数据的类,CGImageRef:图像中的数据

2、CIFilter:滤镜类,用来创建滤镜。对图像的属性进行细节处理。它对所有的图像进行操作,使用KVC(键值)来设置。

3、CIContext:上下文,是实现对图像处理的具体对象。滤镜对象输出的图像,并不是合成之后的图像。需要使用图像处理的上下文,合并输出图像。

4、效果介绍:100+效果,可以通过attributes查找需要设置的参数内容。


按效果分类(介绍一部分):

 kCICategoryDistortionEffect 扭曲效果,比如bump、旋转、hole

 kCICategoryGeometryAdjustment 几何开着调整,比如仿射变换、平切、透视转换

 kCICategoryCompositeOperation 合并,比如源覆盖(source over)、最小化、源在顶(source atop)、色彩混合模式

 kCICategoryHalftoneEffect Halftone效果,比如screenline screenhatched

 kCICategoryColorAdjustment 色彩调整,比如伽马调整、白点调整、曝光

 kCICategoryColorEffect 色彩效果,比如色调调整、posterize

 kCICategoryTransition 图像间转换,比如dissolvedisintegrate with maskswipe

 kCICategoryTileEffect 瓦片效果,比如parallelogramtriangle

 kCICategoryGenerator 图像生成器,比如stripesconstant colorcheckerboard

 kCICategoryGradient 渐变,比如轴向渐变、仿射渐变、高斯渐变

 kCICategoryStylize 风格化,比如像素化、水晶化

 kCICategorySharpen 锐化、发光

 kCICategoryBlur模糊,比如高斯模糊、焦点模糊、运动模糊

 按使用场景分类:

 kCICategoryStillImage 用于静态图像

 kCICategoryVideo 用于视频

 kCICategoryInterlaced 用于交错图像

 kCICategoryNonSquarePixels 用于非矩形像素

 kCICategoryHighDynamicRange 用于HDR


三、具体的使用

操作步骤:

1、创建CIImage,先把UIImage转换成CGImageRef,然后在转换成CIImage

2、创建CIFilter滤镜,并给滤镜设置属性(使用KVC)根据查询到的属性

3、创建CIFilter上下文

4、合并滤镜输出的图像

5、赋值给UIImageView对象进行显示

6、如果想使用滤镜链,可以叠加使用


查询效果分类中所包含的效果:使用

filterNamesInCategory

(1)按下command点击进入接口文件,找到128-148行,全部都是效果分类

(2)选择其中某一个分类NSLog

[CIFilter filterNamesInCategory:刚才拷贝的分类]查看打印结果,是这个类包含的所有效果,拷贝其中一个效果,进    行下一步查询。

(3)查询使用的效果中可以设置什么属性,用filter.attributes查询,设置属性(KVC)例如;

[filter setValue:[CIVectorvectorWithX:150 Y:150]forKey:kCIInputCenterKey];


具体代码

#import "ViewController.h"

@interfaceViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>

{

    UIImageView *myImageView;

}

@end

@implementation ViewController

- (void)viewDidLoad {

    [superviewDidLoad];    

    myImageView = [[UIImageViewalloc] initWithFrame:[UIScreenmainScreen].bounds];

    myImageView.contentMode = UIViewContentModeScaleAspectFit;

    [self.viewaddSubview:myImageView];

 //创建允许访问相册的按钮

    UIButton *button = [UIButtonbuttonWithType:UIButtonTypeCustom];

    button.frame =CGRectMake(50,50, 300,50);

    button.backgroundColor = [UIColorcyanColor];

    [button setTitle:@"相册"forState:UIControlStateNormal];

    [button addTarget:selfaction:@selector(doit)forControlEvents:UIControlEventTouchDown];

    [self.viewaddSubview:button];

//创建控制滤镜效果的按钮

    UIButton *button2 = [UIButtonbuttonWithType:UIButtonTypeCustom];

    button2.frame =CGRectMake(50,120, 300,50);

    button2.backgroundColor = [UIColorcyanColor];

    [button2 setTitle:@"滤镜"forState:UIControlStateNormal]; 

    [button addTarget:sel action:@selector(addColorFiler)

    forControlEvents:UIControlEventTouchDown];

    [self.viewaddSubview:button2];

}

//请求访问相册

- (void)doit{

    UIImagePickerController *picker = [[UIImagePickerControlleralloc] init];

    picker.delegate =self;

    [selfpresentViewController:pickeranimated:YEScompletion:nil];

}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{

    UIImage *chooseImage = info[UIImagePickerControllerOriginalImage];

    myImageView.image = chooseImage;

    [selfdismissViewControllerAnimated:YEScompletion:nil];

//    NSLog(@"%@",info);

}

//凹凸效果(其他效果以此类推)

- (void)addFilter{


//   1输入的原图,先得到CGImage->Image

    CIImage *inputImage = [CIImageimageWithCGImage:myImageView.image.CGImage];

//  2、滤镜

    CIFilter *filter = [CIFilterfilterWithName:@"CIBumpDistortion"];

    

//   kCIInputImageKey 通过打印可以设置属性里面得到可以设置inputImage->在接口文件里面查找得到一个KEY

    [filter setValue:inputImageforKey:kCIInputImageKey];

//    设置凹凸效果的半径,越大越明显

    [filter setValue:@(500)forKey:kCIInputRadiusKey];

//    CIVector:表示XY坐标的类

//    设置中心点

    [filter setValue:[CIVectorvectorWithX:200Y:200]forKey:kCIInputCenterKey];

    

    [filter setValue:@(-1)forKey:kCIInputScaleKey];

//    3CIContext合并原图和滤镜效果

    CIImage *outputImage = filter.outputImage;

    CIContext *context = [CIContextcontextWithOptions:nil];

//    合并成一个包含原图和滤镜效果的图像

//    参数image:滤镜输出的图像

//    fromRect:合成之后图像的尺寸,图像.extent

   CGImageRef imageRef = [contextcreateCGImage:outputImagefromRect:outputImage.extent];

    

       myImageView.image = [UIImageimageWithCGImage:imageRef];

}


//万花筒效果

- (void)addTitle{

//    1、原图

    CIImage *inputImage = [CIImageimageWithCGImage:myImageView.image.CGImage];

//   2滤镜

    CIFilter *filter = [CIFilterfilterWithName:@"CIKaleidoscope"];

    NSLog(@"%@",[CIFilterfilterNamesInCategory:kCICategoryTileEffect]);

    

    [filter setValue:inputImageforKey:kCIInputImageKey];

    

    [filter setValue:[CIVectorvectorWithX:150 Y:150]forKey:kCIInputCenterKey];

    NSLog(@"%@",filter.attributes);

//  3、合成

    CIContext *context = [CIContextcontextWithOptions:nil];

    CIImage *outputImage = filter.outputImage;

    

    CGImageRef imageRef = [contextcreateCGImage:outputImagefromRect:outputImage.extent];

    myImageView.image = [UIImageimageWithCGImage:imageRef];

}


   滤镜链

- (void)addColorFiler{

    CIImage *inputImage = [CIImageimageWithCGImage:myImageView.image.CGImage];


    CIFilter *filer = [CIFilterfilterWithName:@"CIColorMonochrome"];


    NSLog(@"%@",filer.attributes);


    [filer setValue:inputImageforKey:kCIInputImageKey];


    CIImage *outputImage = filer.outputImage;

    [selfaddFiterLinkerWithImage:outputImage];

}


//再次添加滤镜->形成滤镜链

- (void)addFiterLinkerWithImage:(CIImage *)image{

    

    CIFilter *filter = [CIFilterfilterWithName:@"CISepiaTone"];

    

    [filter setValue:imageforKey:kCIInputImageKey];

    

    [filter setValue:@0.5forKey:kCIInputIntensityKey];

    

    CIContext *context = [CIContextcontextWithOptions:nil];

    

   CGImageRef resultImage = [contextcreateCGImage:filter.outputImagefromRect:filter.outputImage.extent];

    

    myImageView.image = [UIImageimageWithCGImage:resultImage];

    

    NSLog(@"%@",filter.attributes);

    

//    把添加好滤镜效果的图片保存到相册

//    不可以直接保存outputImage这是一个没有把滤镜效果和原图合成的图形

    UIImageWriteToSavedPhotosAlbum(myImageView.image,self, @selector(image:didFinishSavingWithError:contextInfo:),nil);

}


- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{

    NSLog(@"保存成功");

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值