一、Core Image 簡介
Core Image is an image processing and analysis technology designed to provide near real-time processing for still and video images. It operates on image data types from the Core Graphics, Core Video, and Image I/O frameworks, using either a GPU or CPU rendering path. Core Image hides the details of low-level graphics processing by providing an easy-to-use application programming interface (API). You don’t need to know the details of OpenGL or OpenGL ES to leverage the power of the GPU, nor do you need to know anything about Grand Central Dispatch (GCD) to get the benefit of multicore processing. Core Image handles the details for you.
以上是蘋果官方文檔對 Core Image 的介紹,大意是:Core Image 是一種為靜態(tài)圖像和視頻提供處理和分析的技術(shù)。它可以使用 GPU/CPU 的方式對圖像進行處理。Core Image 提供了簡潔的 API 給用戶,隱藏了圖像處理中復(fù)雜的底層內(nèi)容。你可以在不了解 OpenGL、OpenGL ES 甚至是 GCD 的基礎(chǔ)上對其進行使用,它已經(jīng)幫你對這些復(fù)雜的內(nèi)容進行處理了。
在 iOS 中,Core Image 框架是 iOS5 新加入到 iOS 平臺的圖像處理框架,提供了強大、高效的圖像處理方式,用來對基于像素的圖像進行操作與分析,內(nèi)置了很多強大的濾鏡(Filter)(目前數(shù)量超過了180個)。這些濾鏡提供了各種各樣的效果,并且可以通過濾鏡鏈將各種效果疊加起來,形成強大的自定義效果。

二、Core Image 框架使用

Core Image 的 API 主要就是三類:
- CIImage:保存圖像數(shù)據(jù)的類??梢酝ㄟ^ UIImage、圖像文件或者像素數(shù)據(jù)來創(chuàng)建,包括未處理的像素數(shù)據(jù)。
- CIFilter:表示應(yīng)用的濾鏡??蚣苤袑D片屬性進行細節(jié)處理的類。它對所有的像素進行操作,用一些鍵值設(shè)置來決定具體操作的程度。
- CIContext:表示上下文,如 Core Graphics 以及 Core Data 中的上下文用于處理繪制渲染以及處理托管對象一樣,Core Image 的上下文也是實現(xiàn)對圖像處理的具體對象。可以從其中取得圖片的信息。
三、濾鏡和濾鏡鏈
一個濾鏡是一個對象,有很多輸入和輸出,并執(zhí)行一些變化。例如,模糊濾鏡可能需要輸入圖像和模糊半徑來產(chǎn)生適當(dāng)?shù)哪:蟮妮敵鰣D像。
一個濾鏡鏈?zhǔn)且粋€鏈接在一起的濾鏡網(wǎng)絡(luò),使得一個濾鏡的輸出可以是另一個濾鏡的輸入。以這種方式,可以實現(xiàn)精心制作的效果。
高斯模糊效果示例:
+ (UIImage *)gaussianBlurImageWithImageName:(NSString *)imageName {
//1.創(chuàng)建CIImage對象
CIImage *ciImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:imageName]];
//2.創(chuàng)建濾鏡對象(高斯模糊),并設(shè)置參數(shù)
CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
[filter setValue:ciImage forKey:kCIInputImageKey];
[filter setValue:@(15) forKey:@"inputRadius"];
//3.渲染并輸出到CIImage
CIImage *outputImage = filter.outputImage;
//4.創(chuàng)建繪制上下文對象
CIContext *context = [CIContext contextWithOptions:nil];
//5.創(chuàng)建輸出CGImage,注意是耗時操作
CGImageRef cgImage = [context createCGImage:outputImage fromRect:outputImage.extent];
UIImage *image = [UIImage imageWithCGImage:cgImage];
//6.釋放CGImage
CGImageRelease(cgImage);
return image;
}
效果如圖:

四、濾鏡的獲取以及屬性設(shè)置
Core Image 框架中包含了很多濾鏡,那么我們?nèi)绾沃谰唧w有哪些濾鏡呢?又如何知道每一種濾鏡的具體用法呢?我們可以從 CIFilter 的頭文件中找到以 kCICategory 開頭的濾鏡分類,然后通過 CIFilter 提供的類方法得到對應(yīng)分類下的所有濾鏡。以 kCICategoryBlur 這個分類為例:
NSArray *filters = [CIFilter filterNamesInCategory:kCICategoryBlur];
NSLog(@"filters: %@", filters);
/*
打印結(jié)果:
filters: (
CIBokehBlur,
CIBoxBlur,
CIDepthBlurEffect,
CIDiscBlur,
CIGaussianBlur,
CIMaskedVariableBlur,
CIMedianFilter,
CIMorphologyGradient,
CIMorphologyMaximum,
CIMorphologyMinimum,
CIMotionBlur,
CINoiseReduction,
CIZoomBlur
)*/
上面 filters 打印出來的字符串就是各種不同的濾鏡效果,再通過查看濾鏡的屬性來了解它的用法。以高斯模糊為例:
CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
NSLog(@"attributes: %@", filter.attributes);
/*
打印結(jié)果:
attributes: {
"CIAttributeFilterAvailable_Mac" = "10.4";
"CIAttributeFilterAvailable_iOS" = 6;
CIAttributeFilterCategories = (
CICategoryBlur,
CICategoryStillImage,
CICategoryVideo,
CICategoryBuiltIn
);
CIAttributeFilterDisplayName = "Gaussian Blur";
CIAttributeFilterName = CIGaussianBlur;
CIAttributeReferenceDocumentation = "http://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html#//apple_ref/doc/filter/ci/CIGaussianBlur";
inputImage = {
CIAttributeClass = CIImage;
CIAttributeDescription = "The image to use as an input image. For filters that also use a background image, this is the foreground image.";
CIAttributeDisplayName = Image;
CIAttributeType = CIAttributeTypeImage;
};
inputRadius = {
CIAttributeClass = NSNumber;
CIAttributeDefault = 10;
CIAttributeDescription = "The radius determines how many pixels are used to create the blur. The larger the radius, the blurrier the result.";
CIAttributeDisplayName = Radius;
CIAttributeIdentity = 0;
CIAttributeMin = 0;
CIAttributeSliderMax = 100;
CIAttributeSliderMin = 0;
CIAttributeType = CIAttributeTypeScalar;
};
}
*/
屬性說明:
CIAttributeFilterAvailable_Mac:最低支持的 Mac 系統(tǒng)版本
CIAttributeFilterAvailable_iOS:最低支持的 iOS 系統(tǒng)版本
CIAttributeFilterCategories:效果所屬的分類
CIAttributeFilterDisplayName:效果所顯示的名稱
CIAttributeFilterName:效果名字
CIAttributeReferenceDocumentation:效果文檔地址
inputImage:輸入圖像屬性說明(CIImage 對象)
inputRadius:輸入模糊半徑屬性說明(NSNumber 對象)。默認(rèn)模糊度為10,最低為0,最高為100。
五、CIContext 對象
在 iOS 平臺下創(chuàng)建 CIContext 的方式有多種,比較常用的有兩種
- 創(chuàng)建基于 GPU 的 CIContext 對象
CIContext *context = [CIContext contextWithOptions:@{kCIContextUseSoftwareRenderer: @(YES)}];
- 創(chuàng)建基于 GPU 的 CIContext 對象
CIContext *context = [CIContext contextWithOptions:nil];
注意:
采用基于 GPU 的 CIContext 將可以獲得更好的性能,但是基于 GPU 創(chuàng)建的 CIContext 跨應(yīng)用使用時,會自動降為基于 CPU 的。比如:在進入相冊選擇相片的時候,如果在 UIImagePickerControllerDelegate 的代理方法中使用 CIContext 對象處理圖像,系統(tǒng)就會把這個任務(wù)交給 CPU 做。