CoreImage框架是一個專門用來對圖片進行處理的框架,其中提供了許多高級功能衩椒,可以幫助開發(fā)者完成UIKit或者CoreGraphics框架無法完成的任務振劳,并且使用CoreImage框架可以十分輕松的實現(xiàn)濾鏡以及圖像識別等流行技術。本篇博客主要介紹和總結(jié)CoreImage框架的使用奋献,并提供范例代碼耕餐。
一、圖像過濾器
1.幾組內(nèi)置的過濾器
CIFilter是CoreImage中提供的圖像過濾器辟狈,也可以將其理解為濾鏡肠缔。許多美顏應用,圖像處理應用等都是為原圖添加了濾鏡效果哼转。本節(jié)我們著重看下與這個類相關的應用明未。首先,CoreImaghe默認提供了非常多的濾鏡效果壹蔓,但是并沒有詳細的文檔介紹趟妥,有關濾鏡效果可以分為下面幾個類別:
//通過改變圖像的幾何形狀來創(chuàng)建3D效果,類似隆起 過濾器組
NSString * const kCICategoryDistortionEffect;
//旋轉(zhuǎn)扭曲相關過濾器組
NSString * const kCICategoryGeometryAdjustment;
//混合過濾器組 對兩個圖像進行混合操作
NSString * const kCICategoryCompositeOperation;
//一種色調(diào)過濾器組 類似報紙風格
NSString * const kCICategoryHalftoneEffect;
//顏色過濾器組 調(diào)整對比度 亮度等
NSString * const kCICategoryColorEffect;
//多個圖像源的過濾器
NSString * const kCICategoryTransition;
//平鋪圖像過濾器
NSString * const kCICategoryTileEffect;
//濾光類過濾器 通常作為其他過濾器的輸入
NSString * const kCICategoryGenerator;
//減弱圖像數(shù)據(jù)的過濾器 通常用來進行圖像分析
NSString * const kCICategoryReduction;
//漸變過濾器
NSString * const kCICategoryGradient;
//畫像過濾器
NSString * const kCICategoryStylize;
//銳化過濾器
NSString * const kCICategorySharpen;
//模糊過濾器
NSString * const kCICategoryBlur;
//視頻圖片相關過濾器
NSString * const kCICategoryVideo;
//靜態(tài)圖片相關過濾器
NSString * const kCICategoryStillImage;
//交叉圖像過濾器
NSString * const kCICategoryInterlaced;
//非矩形圖像上的過濾器
NSString * const kCICategoryNonSquarePixels;
//高動態(tài)圖像的過濾器
NSString * const kCICategoryHighDynamicRange;
//CoreImage內(nèi)置的過濾器
NSString * const kCICategoryBuiltIn;
//復合的過濾器
NSString * const kCICategoryFilterGenerator;
上面列出了非常多的類別佣蓉,其實上面只是按照不同的場景將過濾器進行了分類披摄,每個分類中都定義了許多內(nèi)置的過濾器,使用下面的方法可以獲取每個分類下提供的過濾器:
//獲取某個分類的所有過濾器名
+ (NSArray<NSString *> *)filterNamesInCategory:(nullable NSString *)category;
//獲取一組分類下的所有過濾器名
+ (NSArray<NSString *> *)filterNamesInCategories:(nullable NSArray<NSString *> *)categories;
2.過濾器的一個簡單示例
下面示例代碼演示過濾器的簡單應用:
UIImage * img = [UIImage imageNamed:@"1.png"];
CIImage * image = [[CIImage alloc]initWithImage:img];
CIFilter * filter = [CIFilter filterWithName:@"CIBoxBlur" keysAndValues:kCIInputImageKey,image, nil];
[filter setDefaults];
CIContext * context = [[CIContext alloc]initWithOptions:nil];
CIImage * output = [filter outputImage];
CGImageRef ref = [context createCGImage:output fromRect:[output extent]];
UIImage * newImage = [UIImage imageWithCGImage:ref];
CGImageRelease(ref);
UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(170, 30, 150, 400)];
imageView.image = newImage;
[self.view addSubview:imageView];
UIImageView * imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 30, 150, 400)];
imageView2.image = img;
[self.view addSubview:imageView2];
效果如下圖:
上面演示了簡單的模糊過濾效果检柬。
3.對CIFilter類進行解析
CIFilter類的解析如下:
//過濾后輸出的圖像
@property (readonly, nonatomic, nullable) CIImage *outputImage;
//過濾器名稱
@property (nonatomic, copy) NSString *name;
//是否開啟CoreAnimation動畫效果
@property (getter=isEnabled) BOOL enabled;
//返回當前過濾器所有支持的輸入鍵
@property (nonatomic, readonly) NSArray<NSString *> *inputKeys;
//返回當前過濾器所有支持的輸出鍵
@property (nonatomic, readonly) NSArray<NSString *> *outputKeys;
//將過濾器的所有輸入值設置為默認值
- (void)setDefaults;
//返回當前過濾器的屬性字段
/*
需要注意這個字段對于學習此過濾器非常有用
其中會聲明此過濾器的輸入和輸出 即如果使用
*/
@property (nonatomic, readonly) NSDictionary<NSString *,id> *attributes;
//用來進行過濾器的自定義 后面會介紹
- (nullable CIImage *)apply:(CIKernel *)k
arguments:(nullable NSArray *)args
options:(nullable NSDictionary<NSString *,id> *)dict;
//同上
- (nullable CIImage *)apply:(CIKernel *)k, ...;
//根據(jù)過濾器的名稱創(chuàng)建過濾器
+ (nullable CIFilter *) filterWithName:(NSString *) name;
//創(chuàng)建過濾器 同時進行配置
+ (nullable CIFilter *)filterWithName:(NSString *)name
keysAndValues:key0, ...;
+ (nullable CIFilter *)filterWithName:(NSString *)name
withInputParameters:(nullable NSDictionary<NSString *,id> *)params;
//注冊過濾器
+ (void)registerFilterName:(NSString *)name
constructor:(id<CIFilterConstructor>)anObject
classAttributes:(NSDictionary<NSString *,id> *)attributes;
//將一組過濾器進行編碼
+ (nullable NSData*)serializedXMPFromFilters:(NSArray<CIFilter *> *)filters
inputImageExtent:(CGRect)extent;
//進行反編碼
+ (NSArray<CIFilter *> *)filterArrayFromSerializedXMP:(NSData *)xmpData
inputImageExtent:(CGRect)extent
error:(NSError **)outError;
4.常用過濾器詳解
- 區(qū)域凸起過濾器
這個過濾器的作用是在圖片的某個區(qū)域創(chuàng)建一塊凸起品擎。示例代碼如下:
/*
kCIInputCenterKey鍵用來設置濾鏡中心
kCIInputScaleKey 設置為0則沒有影響 1則會凸起效果 -1則會凹入效果
kCIInputRadiusKey 設置濾鏡的影響范圍
*/
CIFilter * filter = [CIFilter filterWithName:@"CIBumpDistortion" keysAndValues:kCIInputImageKey,image,kCIInputCenterKey,[[CIVector alloc] initWithX:100 Y:200],kCIInputScaleKey,@-1,kCIInputRadiusKey,@150, nil];
效果如下:
- 線性凹凸過濾器
這個過濾器創(chuàng)建類似波紋效果随闺,示例如下:
/*
與上一個過濾器相比 可以設置
kCIInputAngleKey 角度 0-2π
*/
CIFilter * filter = [CIFilter filterWithName:@"CIBumpDistortionLinear" keysAndValues:kCIInputImageKey,image,kCIInputCenterKey,[[CIVector alloc] initWithX:100 Y:200],kCIInputScaleKey,@-1,kCIInputRadiusKey,@150,kCIInputAngleKey,@(M_PI_2), nil];
效果如下:
- 圓形飛濺過濾器
這個過濾器的作用是選取圖像的某個區(qū)域,對其四周進行飛濺拉伸寓盗,例如:
CIFilter * filter = [CIFilter filterWithName:@"CICircleSplashDistortion" keysAndValues:kCIInputImageKey,image,kCIInputCenterKey,[[CIVector alloc] initWithX:100 Y:200],kCIInputRadiusKey,@50, nil];
效果如下:
- 圓形纏繞過濾器
這個過濾器選取某個區(qū)域,進行纏繞效果璧函,例如:
CIFilter * filter = [CIFilter filterWithName:@"CICircularWrap" keysAndValues:kCIInputImageKey,image,kCIInputCenterKey,[[CIVector alloc] initWithX:100 Y:200],kCIInputRadiusKey,@20, kCIInputAngleKey,@3,nil];
效果如下:
- 灰度混合過濾器
這個過濾器將提供混合圖像的灰度值應用于目標圖像傀蚌,例如:
/*
inputDisplacementImage設置要混合的灰度圖片
*/
CIFilter * filter = [CIFilter filterWithName:@"CIDroste" keysAndValues:kCIInputImageKey,image,kCIInputScaleKey,@200,@"inputDisplacementImage",image2,nil];
效果如下:
- 遞歸繪制圖像區(qū)域
CIFilter * filter = [CIFilter filterWithName:@"CIDroste" keysAndValues:kCIInputImageKey,image,@"inputInsetPoint0",[[CIVector alloc] initWithX:100 Y:100],@"inputInsetPoint1",[[CIVector alloc] initWithX:200 Y:200],@"inputPeriodicity",@1,@"inputRotation",@0,@"inputStrands",@1,@"inputZoom",@1,nil];
效果如下:
- 玻璃紋理過濾器
這個過濾器用提供圖片作為目標圖片的紋理,進行混合蘸吓,例如:
/*
inputTexture設置紋理圖像
*/
CIFilter * filter = [CIFilter filterWithName:@"CIGlassDistortion" keysAndValues:kCIInputImageKey,image,kCIInputCenterKey,[[CIVector alloc] initWithX:100 Y:200],kCIInputScaleKey,@100,@"inputTexture",image2,nil];
效果如下:
- 菱形透鏡過濾器
/*
inputPoint0設置第一個圓的圓心
inputPoint1設置第二個圓的圓心
inputRadius設置半徑
inputRefraction設置折射率 0-5之間
*/
CIFilter * filter = [CIFilter filterWithName:@"CIGlassLozenge" keysAndValues:kCIInputImageKey,image,@"inputPoint0",[[CIVector alloc] initWithX:100 Y:200],@"inputPoint1",[[CIVector alloc] initWithX:200 Y:200],@"inputRadius",@100,@"inputRefraction",@2,nil];
效果如下:
- 圓孔形變過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIHoleDistortion" keysAndValues:kCIInputImageKey,image,@"inputRadius",@50,kCIInputCenterKey,[[CIVector alloc] initWithX:100 Y:200],nil];
效果如下:
- 九宮格拉伸過濾器
CIFilter * filter = [CIFilter filterWithName:@"CINinePartStretched" keysAndValues:kCIInputImageKey,image2,@"inputBreakpoint0",[[CIVector alloc] initWithX:50 Y:50],@"inputBreakpoint1",[[CIVector alloc] initWithX:100 Y:100],@"inputGrowAmount",[[CIVector alloc] initWithX:50 Y:50],nil];
效果如下:
- 九宮格復制過濾器
CIFilter * filter = [CIFilter filterWithName:@"CINinePartTiled" keysAndValues:kCIInputImageKey,image2,@"inputBreakpoint0",[[CIVector alloc] initWithX:50 Y:50],@"inputBreakpoint1",[[CIVector alloc] initWithX:100 Y:100],@"inputGrowAmount",[[CIVector alloc] initWithX:50 Y:50],@"inputFlipYTiles",@1,nil];
效果如下:
- 緊縮過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIPinchDistortion" keysAndValues:kCIInputImageKey,image2,@"inputCenter",[[CIVector alloc] initWithX:150 Y:150],@"inputRadius",@500,@"inputScale",@1,nil];
效果如下:
- 拉伸裁剪過濾器
/*
inputSize 設置拉伸裁剪尺寸
*/
CIFilter * filter = [CIFilter filterWithName:@"CIStretchCrop" keysAndValues:kCIInputImageKey,image2,@"inputCenterStretchAmount",@1,@"inputCropAmount",@0.5,@"inputSize",[[CIVector alloc] initWithX:300 Y:150],nil];
效果如下:
- 環(huán)狀透鏡過濾器
這個過濾器創(chuàng)建一個環(huán)狀透鏡善炫,對圖像進行扭曲。
/*
inputCenter設置環(huán)中心
inputRadius 設置半徑
inputRefraction 設置折射率
inputWidth 設置環(huán)寬度
*/
CIFilter * filter = [CIFilter filterWithName:@"CITorusLensDistortion" keysAndValues:kCIInputImageKey,image2,@"inputCenter",[[CIVector alloc] initWithX:150 Y:150],@"inputRadius",@150,@"inputRefraction",@1.6,@"inputWidth",@40,nil];
效果如下:
- 旋轉(zhuǎn)過濾器
CIFilter * filter = [CIFilter filterWithName:@"CITwirlDistortion" keysAndValues:kCIInputImageKey,image2,@"inputAngle",@3.14,@"inputCenter",[[CIVector alloc] initWithX:150 Y:150],@"inputRadius",@150,nil];
效果如下:
- 渦流過濾器
//inputAngle 設置渦流角度
CIFilter * filter = [CIFilter filterWithName:@"CIVortexDistortion" keysAndValues:kCIInputImageKey,image2,@"inputAngle",@(M_PI*10),@"inputCenter",[[CIVector alloc] initWithX:150 Y:150],@"inputRadius",@150,nil];
效果如下:
- 形變過濾器
這個過濾器對圖像進行簡單的形變處理美澳,如縮放销部,旋轉(zhuǎn),平移等制跟。
CGAffineTransform tr = CGAffineTransformMakeRotation(M_PI_2);
CIFilter * filter = [CIFilter filterWithName:@"CIAffineTransform" keysAndValues:kCIInputImageKey,image2,@"inputTransform",[NSValue valueWithCGAffineTransform:tr],nil];
效果如下:
- 矩形裁剪過濾器
CIFilter * filter = [CIFilter filterWithName:@"CICrop" keysAndValues:kCIInputImageKey,image2,@"inputRectangle",[[CIVector alloc] initWithCGRect:CGRectMake(0, 0, 150, 150)],nil];
效果如下:
- 邊緣采樣過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIEdgePreserveUpsampleFilter" keysAndValues:kCIInputImageKey,image,@"inputLumaSigma",@0.15,@"inputSpatialSigma",@3,@"inputSmallImage",image2,nil];
效果如下:
- 矩形矯正過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIPerspectiveCorrection" keysAndValues:kCIInputImageKey,image2,@"inputBottomLeft",[[CIVector alloc] initWithX:0 Y:0],@"inputBottomRight",[[CIVector alloc] initWithX:150 Y:0],@"inputTopLeft",[[CIVector alloc] initWithX:0 Y:150],@"inputTopRight",[[CIVector alloc] initWithX:150 Y:150],nil];
效果如圖:
- 旋轉(zhuǎn)矯正過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIStraightenFilter" keysAndValues:kCIInputImageKey,image2,@"inputAngle",@3.14,nil];
效果如下:
- 背景混合過濾器
通過提供一個圖像作為背景與目標圖像進行混合舅桩。
CIFilter * filter = [CIFilter filterWithName:@"CIAdditionCompositing" keysAndValues:kCIInputImageKey,image2,@"inputBackgroundImage",image,nil];
效果如下:
- 色彩混合過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIColorBlendMode" keysAndValues:kCIInputImageKey,image2,@"inputBackgroundImage",image,nil];
效果如下:
- 暗混合過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIColorBurnBlendMode" keysAndValues:kCIInputImageKey,image2,@"inputBackgroundImage",image,nil];
效果如下:
- 亮混合過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIColorDodgeBlendMode" keysAndValues:kCIInputImageKey,image2,@"inputBackgroundImage",image,nil];
- 暗選擇混合模式過濾器
這個過濾器將選擇較暗的圖像作為混合背景,例如:
CIFilter * filter = [CIFilter filterWithName:@"CIDarkenBlendMode" keysAndValues:kCIInputImageKey,image2,@"inputBackgroundImage",image,nil];
效果如下:
- 亮選擇混合模式過濾器
這個過濾器將選擇較亮的圖像作為混合背景雨膨,例如:
CIFilter * filter = [CIFilter filterWithName:@"CIDifferenceBlendMode" keysAndValues:kCIInputImageKey,image2,@"inputBackgroundImage",image,nil];
效果如下:
- 分開混合模式過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIDivideBlendMode" keysAndValues:kCIInputImageKey,image2,@"inputBackgroundImage",image,nil];
效果如下:
- 排除混合模式過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIExclusionBlendMode" keysAndValues:kCIInputImageKey,image2,@"inputBackgroundImage",image,nil];
效果如下:
- 強光混合模式過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIHardLightBlendMode" keysAndValues:kCIInputImageKey,image2,@"inputBackgroundImage",image,nil];
效果如下:
- 色調(diào)混合模式過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIHueBlendMode" keysAndValues:kCIInputImageKey,image2,@"inputBackgroundImage",image,nil];
效果如下:
- 減輕混合模式過濾器
CIFilter * filter = [CIFilter filterWithName:@"CILightenBlendMode" keysAndValues:kCIInputImageKey,image2,@"inputBackgroundImage",image,nil];
效果如下:
- 線性燃燒混合模式過濾器
CIFilter * filter = [CIFilter filterWithName:@"CILinearBurnBlendMode" keysAndValues:kCIInputImageKey,image2,@"inputBackgroundImage",image,nil];
效果如下:
- 線性高亮混合過濾器
CIFilter * filter = [CIFilter filterWithName:@"CILinearDodgeBlendMode" keysAndValues:kCIInputImageKey,image2,@"inputBackgroundImage",image,nil];
效果如下:
- 亮度混合過濾器
CIFilter * filter = [CIFilter filterWithName:@"CILuminosityBlendMode" keysAndValues:kCIInputImageKey,image2,@"inputBackgroundImage",image,nil];
效果如下:
- 最大值混合過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIMaximumCompositing" keysAndValues:kCIInputImageKey,image2,@"inputBackgroundImage",image,nil];
效果如下:
- 最小值混合過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIMinimumCompositing" keysAndValues:kCIInputImageKey,image2,@"inputBackgroundImage",image,nil];
效果如下:
- 多重混合過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIMultiplyBlendMode" keysAndValues:kCIInputImageKey,image2,@"inputBackgroundImage",image,nil];
效果如下:
- 多重合成過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIMultiplyCompositing" keysAndValues:kCIInputImageKey,image2,@"inputBackgroundImage",image,nil];
- 重疊混合模式
CIFilter * filter = [CIFilter filterWithName:@"CIOverlayBlendMode" keysAndValues:kCIInputImageKey,image2,@"inputBackgroundImage",image,nil];
- 亮混合模式
CIFilter * filter = [CIFilter filterWithName:@"CIPinLightBlendMode" keysAndValues:kCIInputImageKey,image2,@"inputBackgroundImage",image,nil];
- 飽和混合模式
CIFilter * filter = [CIFilter filterWithName:@"CISaturationBlendMode" keysAndValues:kCIInputImageKey,image2,@"inputBackgroundImage",image,nil];
- 屏幕混合模式
CIFilter * filter = [CIFilter filterWithName:@"CIScreenBlendMode" keysAndValues:kCIInputImageKey,image2,@"inputBackgroundImage",image,nil];
- 源圖像上層混合
CIFilter * filter = [CIFilter filterWithName:@"CISourceAtopCompositing" keysAndValues:kCIInputImageKey,image2,@"inputBackgroundImage",image,nil];
- 圓屏過濾器
/*
inputSharpness 設置圓圈銳度
inputWidth 設置間距
*/
CIFilter * filter = [CIFilter filterWithName:@"CICircularScreen" keysAndValues:kCIInputImageKey,image2,kCIInputCenterKey,[[CIVector alloc] initWithX:150 Y:150],@"inputSharpness",@0.7,@"inputWidth",@6,nil];
- 半色調(diào)過濾器
CIFilter * filter = [CIFilter filterWithName:@"CICMYKHalftone" keysAndValues:kCIInputImageKey,image2,@"inputAngle",@0,kCIInputCenterKey,[[CIVector alloc] initWithX:150 Y:150],@"inputGCR",@1,@"inputSharpness",@0.7,@"inputUCR",@0.5,@"inputWidth",@6,nil];
- 點屏過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIDotScreen" keysAndValues:kCIInputImageKey,image2,@"inputAngle",@0,kCIInputCenterKey,[[CIVector alloc] initWithX:150 Y:150],@"inputSharpness",@0.7,@"inputWidth",@6,nil];
- 陰影屏過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIHatchedScreen" keysAndValues:kCIInputImageKey,image2,@"inputAngle",@0,kCIInputCenterKey,[[CIVector alloc] initWithX:150 Y:150],@"inputSharpness",@0.7,@"inputWidth",@6,nil];
- 線性sRGB過濾器
CIFilter * filter = [CIFilter filterWithName:@"CILinearToSRGBToneCurve" keysAndValues:kCIInputImageKey,image2,nil];
- 色彩翻轉(zhuǎn)過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIColorInvert" keysAndValues:kCIInputImageKey,image2,nil];
- 色圖過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIColorMap" keysAndValues:kCIInputImageKey,image2,@"inputGradientImage",image,nil];
- 單色過濾器
/*
inputColor 設置輸入顏色
inputIntensity 設置影響程度
*/
CIFilter * filter = [CIFilter filterWithName:@"CIColorMonochrome" keysAndValues:kCIInputImageKey,image2,@"inputColor",[CIColor colorWithRed:0.5 green:0.5 blue:0.5],@"inputIntensity",@1,nil];
- 分色鏡過濾器
/*
inputLevels設置亮度級別
*/
CIFilter * filter = [CIFilter filterWithName:@"CIColorPosterize" keysAndValues:kCIInputImageKey,image2,@"inputLevels",@6,nil];
- 反色過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIFalseColor" keysAndValues:kCIInputImageKey,image2,@"inputColor0",[CIColor colorWithRed:0 green:0 blue:0],@"inputColor1",[CIColor colorWithRed:1 green:1 blue:0],nil];
- 光效褪色過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIPhotoEffectFade" keysAndValues:kCIInputImageKey,image2,nil];
- 光效瞬時過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIPhotoEffectInstant" keysAndValues:kCIInputImageKey,image2,nil];
- 光效單光過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIPhotoEffectMono" keysAndValues:kCIInputImageKey,image2,nil];
- 黑色光效應過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIPhotoEffectNoir" keysAndValues:kCIInputImageKey,image2,nil];
- 光漸進過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIPhotoEffectProcess" keysAndValues:kCIInputImageKey,image2,nil];
- 光轉(zhuǎn)移過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIPhotoEffectTransfer" keysAndValues:kCIInputImageKey,image2,nil];
- 棕褐色過濾器
CIFilter * filter = [CIFilter filterWithName:@"CISepiaTone" keysAndValues:kCIInputImageKey,image2,nil];
- 熱圖過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIThermal" keysAndValues:kCIInputImageKey,image2,nil];
- X射線過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIXRay" keysAndValues:kCIInputImageKey,image2,nil];
- 模糊過濾器
//參數(shù)進行模糊效果的設置
CIFilter * filter = [CIFilter filterWithName:@"CIBokehBlur" keysAndValues:kCIInputImageKey,image2,@"inputSoftness",@0.5,@"inputRingSize",@0.1,@"inputRingAmount",@0,@"inputRadius",@10,nil];
- 盒模糊過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIBoxBlur" keysAndValues:kCIInputImageKey,image2,@"inputRadius",@10,nil];
- 閥瓣模糊過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIDiscBlur" keysAndValues:kCIInputImageKey,image2,@"inputRadius",@25,nil];
- 高斯模糊過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIGaussianBlur" keysAndValues:kCIInputImageKey,image2,@"inputRadius",@10,nil];
- 梯度模糊過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIMorphologyGradient" keysAndValues:kCIInputImageKey,image2,@"inputRadius",@5,nil];
- 運動模糊過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIMotionBlur" keysAndValues:kCIInputImageKey,image2,@"inputRadius",@5,nil];
- 縮放模糊過濾器
CIFilter * filter = [CIFilter filterWithName:@"CIZoomBlur" keysAndValues:kCIInputImageKey,image2,nil];
5.自定義過濾器
上面演示了非常多的常用內(nèi)置過濾器擂涛,我們也可以通過繼承CIFilter來自定義過濾器。
自定義過濾器之前,首先需要了解CIKernel這個類撒妈,CIKernel是Core Image Kernel Language 的抽象對象恢暖。CIKL是CoreImage中專門用來編寫像素處理函數(shù)的語言。
CIKernel相關類解析如下:
//基類 用于通用的過濾函數(shù)
@interface CIKernel : NSObject
//從字符串加載一組過濾函數(shù)
+ (nullable NSArray<CIKernel *> *)kernelsWithString:(NSString *)string;
//從字符串加載一個過濾函數(shù)
+ (nullable instancetype)kernelWithString:(NSString *)string ;
//名稱
@property (atomic, readonly) NSString *name ;
//進行圖片生成
- (nullable CIImage *)applyWithExtent:(CGRect)extent
roiCallback:(CIKernelROICallback)callback
arguments:(nullable NSArray<id> *)args;
@end
//用于顏色修正的過濾函數(shù)
@interface CIColorKernel : CIKernel
+ (nullable instancetype)kernelWithString:(NSString *)string;
- (nullable CIImage *)applyWithExtent:(CGRect)extent
arguments:(nullable NSArray<id> *)args;
@end
//用于形狀修正的過濾函數(shù)
@interface CIWarpKernel : CIKernel
+ (nullable instancetype)kernelWithString:(NSString *)string;
@end
//用于色彩混合的過濾函數(shù)
@interface CIBlendKernel : CIColorKernel
+ (nullable instancetype)kernelWithString:(NSString *)string;
- (nullable CIImage *)applyWithForeground:(CIImage*)foreground
background:(CIImage*)background;
@end
下面是一個簡單的翻轉(zhuǎn)圖像的自定義過濾器示意狰右,首先新建一個新的cikernel文件杰捂,命名為a.cikernel,如下:
kernel vec2 mirrorX ( float imageWidth )
{
// 獲取待處理點的位置
vec2 currentVec = destCoord();
// 返回最終顯示位置
return vec2 ( imageWidth - currentVec.x , currentVec.y );
}
新建一個過濾器類棋蚌,命名為MyFilter嫁佳,如下:
#import <CoreImage/CoreImage.h>
@interface MyFilter : CIFilter
@property(nonatomic,strong)CIImage * inputImage;
@end
#import "MyFilter.h"
@interface MyFilter()
@property(nonatomic,strong)CIWarpKernel * kernel;
@end
@implementation MyFilter
- (instancetype)init {
self = [super init];
if (self) {
//從文件讀取過濾函數(shù)
NSBundle *bundle = [NSBundle bundleForClass: [self class]];
NSURL *kernelURL = [bundle URLForResource:@"a" withExtension:@"cikernel"];
NSError *error;
NSString *kernelCode = [NSString stringWithContentsOfURL:kernelURL
encoding:NSUTF8StringEncoding error:&error];
NSArray *kernels = [CIKernel kernelsWithString:kernelCode];
self.kernel = [kernels objectAtIndex:0];
}
return self;
}
- (CIImage *)outputImage
{
CGFloat inputWidth = self.inputImage.extent.size.width;
CIImage *result = [self.kernel applyWithExtent:self.inputImage.extent roiCallback:^CGRect(int index, CGRect destRect) {
return destRect;
} inputImage:self.inputImage arguments:@[@(inputWidth)]];
return result;
}
//設置說明字典
-(NSDictionary<NSString *,id> *)attributes{
return @{
@"inputImage" : @{
@"CIAttributeClass" : @"CIImage",
@"CIAttributeDisplayName" : @"Image--",
@"CIAttributeType" : @"CIAttributeTypeImage"
}};
}
@end
如下進行使用即可:
MyFilter * filter = [[MyFilter alloc]init];
filter.inputImage = image2;
CIContext * context = [[CIContext alloc]initWithOptions:nil];
CIImage * output = [filter outputImage];
CGImageRef ref = [context createCGImage:output fromRect:output.extent];
UIImage * newImage = [UIImage imageWithCGImage:ref];