版本記錄
版本號 | 時間 |
---|---|
V1.0 | 2018.01.28 |
前言
Core Image是IOS5中新加入的一個框架,里面提供了強大高效的圖像處理功能,用來對基于像素的圖像進行操作與分析。還提供了很多強大的濾鏡,可以實現你想要的效果,下面我們就一起解析一下這個框架。感興趣的可以參考上面幾篇。
1. Core Image框架詳細解析(一) —— 基本概覽
2. Core Image框架詳細解析(二) —— Core Image濾波器參考
3. Core Image框架詳細解析(三) —— 關于Core Image
4. Core Image框架詳細解析(四) —— Processing Images處理圖像(一)
5. Core Image框架詳細解析(五) —— Processing Images處理圖像(二)
6. Core Image框架詳細解析(六) —— 圖像中的面部識別Detecting Faces in an Image(一)
7. Core Image框架詳細解析(七) —— 自動增強圖像 Auto Enhancing Images
8. Core Image框架詳細解析(八) —— 查詢系統中的過濾器 Querying the System for Filters
9. Core Image框架詳細解析(九) —— 子類化CIFilter:自定義效果的配方 Subclassing CIFilter: Recipes for Custom Effects(一)
10. Core Image框架詳細解析(十) —— 子類化CIFilter:自定義效果的配方 Subclassing CIFilter: Recipes for Custom Effects(二)
11. Core Image框架詳細解析(十一) —— 獲得最佳性能 Getting the Best Performance
使用反饋來處理圖像
CIImageAccumulator
類非常適合基于反饋的處理。 顧名思義,它會隨著時間的推移累積圖像數據。 本章介紹如何使用CIImageAccumulator
對象來實現一個名為MicroPaint
的簡單繪畫應用程序,該應用程序允許用戶在畫布上繪制類似于圖7-1所示的圖像。
“Image”以空白畫布開始。 MicroPaint
使用圖像收集器來收集用戶所涂的油漆。 當用戶單擊清除時,MicroPaint會將圖像累加器重置為白色畫布。 顏色也允許用戶改變涂料的顏色。 用戶可以使用滑塊更改畫筆大小。
創建MicroPaint應用程序的圖像累加器的基本任務是:
本章僅介紹創建圖像累加器和支持繪制圖形所必需的代碼。 這里不討論繪制視圖和處理視圖大小變化的方法。 為此,請參閱CIMicroPaint,它是一個完整的示例代碼項目,您可以下載并更詳細地檢查。CIMicroPaint
有幾個有趣的細節。 它演示了如何繪制到OpenGL視圖,并保持以前版本的OS X的向后兼容性。
Set Up the Interface for the MicroPaint App - 設置MicroPaint應用程序的界面
MicroPaint
的界面需要以下內容:
- 一個圖像累加器
- 一個“brush”。 畫筆brush是一個Core Image過濾器
(CIRadialGradient)
,以模擬空氣刷的方式應用顏色。 - 一個復合過濾器
(CISourceOverCompositing)
,允許新的油漆合成在以前應用的油漆上。 - 用于跟蹤當前繪畫顏色和筆刷大小的變量。
構建過濾器字典將MircoPaintView
聲明為SampleCIView
的子類。 SampleCIView類不在這里討論。 它是NSOpenGLView
類的一個子類。 有關詳細信息,請參閱CIMicroPaint示例應用程序。
// Listing 7-1 The interface for the MicroPaint app
@interface MicroPaintView : SampleCIView {
CIImageAccumulator *imageAccumulator;
CIFilter *brushFilter;
CIFilter *compositeFilter;
NSColor *color;
CGFloat brushSize;
}
@end
Initialize Filters and Default Values for Painting - 初始化繪畫的過濾器和默認值
當初始化MicroPaint
應用程序(如Listing 7-2所示)時,您需要創建畫筆和復合濾鏡,并設置初始畫筆大小和繪畫顏色。 Listing 7-2中的代碼被創建并初始化為透明的黑色,輸入半徑為0,當用戶拖動光標時,畫筆過濾器將獲取畫筆大小和顏色的當前值。
// Listing 7-2 Initializing filters, brush size, and paint color
brushFilter = [CIFilter filterWithName: @"CIRadialGradient" withInputParameters:@{
@"inputColor1": [CIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0],
@"inputRadius0": @0.0,
}];
compositeFilter = [CIFilter filterWithName: @"CISourceOverCompositing"];
brushSize = 25.0;
color = [NSClor colorWithDeviceRed: 0.0 green: 0.0 blue : 0.0 alpha : 1.0];
Track and Accumulate Painting Operations - 跟蹤和積累繪畫操作
只要用戶在畫布上單擊或拖動光標,就會調用mouseDragged:
方法。 它更新畫筆和合成濾鏡值,并將新的繪畫操作添加到累積的圖像。
設置圖像后,您需要觸發顯示的更新。 drawRect:
方法處理繪制圖像。 繪制到CIContext
對象時,請確保使用drawImage:inRect:fromRect:而不是棄用的方法drawImage:atPoint:fromRect :
。
// Listing 7-3 Setting up and applying the brush filter to the accumulated image
- (void)mouseDragged:(NSEvent *)event
{
CGRect rect;
NSPoint loc = [self convertPoint: [event locationInWindow] fromView: nil];
CIColor *cicolor;
// Make a rectangle that is centered on the drag location and
// whose dimensions are twice of the current brush size
rect = CGRectMake(loc.x-brushSize, loc.y-brushSize,
2.0*brushSize, 2.0*brushSize);
// Set the size of the brush
// Recall this is really a radial gradient filter
[brushFilter setValue: @(brushSize)
forKey: @"inputRadius1"];
cicolor = [[CIColor alloc] initWithColor: color];
[brushFilter setValue: cicolor forKey: @"inputColor0"];
[brushFilter setValue: [CIVector vectorWithX: loc.x Y:loc.y]
forKey: kCIInputCenterKey];
// Composite the output from the brush filter with the image
// accummulated by the image accumulator
[compositeFilter setValue: [brushFilter valueForKey: kCIOutputImageKey]
forKey: kCIInputImageKey];
[compositeFilter setValue: [imageAccumulator image]
forKey: kCIInputBackgroundImageKey];
// Set the image accumluator to the composited image
[imageAccumulator setImage: [compositeFilter valueForKey: kCIOutputImageKey]
dirtyRect: rect];
// After setting the image, you need to trigger an update of the display
[self setImage: [imageAccumulator image] dirtyRect: rect];
}
后記
本篇已結束,后面更精彩~~~