首页 > 其他 > 详细

怎么用CIFilter给图片加上各种各样的滤镜_2

时间:2015-07-04 16:47:56      阅读:446      评论:0      收藏:0      [点我收藏+]

上一篇讲了怎么找到能用的的滤镜和大概怎么去寻找。。。这里接着说怎样具体地给图片加滤镜效果。。前的准备工作。。。。

1. 在找到想用的滤镜名字之后,需要知道这个滤镜到底需要什么参数。。如下图

技术分享

这里打印出来的,就是当前的滤镜的全部参数。。。如下图

技术分享

而在CIAttributeFilterName下面的参数,就是需要设置的属性。。。这里就是inputColor、inputImage、inputIntensity这3个

其中CIAttributeClass表明这个属性需要的是什么类型的值。

设置属性的方法如下:

技术分享

通过使用setValue forKey的方法来设置属性。。。这个key就是k+属性名+Key

在属性设置完之后,就可以输出了!。。这里直接用CIColorMonochrome这个滤镜来做演示。。

- (UIImage *)imageFilter:(UIImage *)photo Value:(float)value
{
    CIContext *context = [CIContext contextWithOptions:nil];               // 1
    CIImage *image = [[CIImage alloc] initWithImage:photo];                // 2
    
    CIFilter *filter = [CIFilter filterWithName:@"CIColorMonochrome"];           // 3
    
    NSLog(@"%@", [filter attributes]);
    
    [filter setValue:image forKey:kCIInputImageKey];
    [filter setValue:[CIColor colorWithRed:100/255 green:0.4 blue:1] forKey:kCIInputColorKey];
    
    CIImage *result = [filter valueForKey:kCIOutputImageKey];              // 4
    CGRect extent = [result extent];
    CGImageRef cgImage = [context createCGImage:result fromRect:extent];// 5
    
    return [UIImage imageWithCGImage:cgImage];
}
到这为止。。。return的UIImage就是叠加上滤镜的图片了。



版权声明:本文为博主原创文章,未经博主允许不得转载。

怎么用CIFilter给图片加上各种各样的滤镜_2

原文:http://blog.csdn.net/u1031/article/details/46755251

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!