版本记录
版本号 | 时间 |
---|---|
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];
}
后记
本篇已结束,后面更精彩~~~