Core Image框架详细解析(八) —— 查询系统中的过滤器 Querying the System for Filters

版本记录

版本号 时间
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

查询系统中的过滤器

Core Image提供的方法可以让您查询系统中可用的内置过滤器以及每个过滤器的显示名称,输入参数,参数类型,默认值等的相关信息。 查询系统可为您提供有关可用过滤器的最新信息。 如果您的应用支持让用户选择并设置过滤器,则可以在为过滤器创建用户界面时使用此信息。


Getting a List of Filters and Attributes - 获取过滤器和属性的列表

使用filterNamesInCategory:和filterNamesInCategories:方法来精确地发现哪些过滤器可用。 过滤器被分类以使列表更易于管理。 如果您知道过滤器类别,则可以通过调用filterNamesInCategory:方法找到该类别可用的过滤器:并提供Table 4-1,Table 4-2或Table 4-3中列出的一个类别常量。

如果要查找类别列表的所有可用过滤器,可以调用filterNamesInCategories:方法,提供从表中列出的类别常量数组。 该方法返回一个NSArray对象,填充每个类别的过滤器名称。 您可以通过提供nil而不是类别常量数组来获得所有类别的所有过滤器的列表。

过滤器可以是多个类别category的成员。 一个类别可以指定:

  • 滤镜产生的效果类型(颜色调整,失真等等)。 见Table 4-1。
  • 过滤器的使用(静止图像,视频,高动态范围等)。 见Table 4-2。
  • 过滤器是否由Core Image提供(内置)。 见 Table 4-3。
Core Image框架详细解析(八) —— 查询系统中的过滤器 Querying the System for Filters_第1张图片
Core Image框架详细解析(八) —— 查询系统中的过滤器 Querying the System for Filters_第2张图片

获取过滤器名称列表后,您可以通过创建CIFilter对象并调用方法attributes来检索过滤器的属性,如下所示:

CIFilter *myFilter = [CIFilter filterWithName:@"<# Filter Name Here #>"];
NSDictionary *myFilterAttributes = [myFilter attributes];

您将字符串“<#Filter Name Here#>”替换为您感兴趣的过滤器的名称。属性包括名称,类别,类,最小值和最大值等。 请参阅CIFilter Class Reference以获取可返回的属性的完整列表。


Building a Dictionary of Filters - 构建一个滤波器字典

如果您的App提供用户界面,则可以查阅过滤器字典来创建和更新用户界面。 例如,布尔型的过滤器属性需要复选框或类似的用户界面元素,而在一定范围内连续变化的属性可以使用滑块。 您可以使用最大值和最小值作为文本标签的基础。 默认属性设置将决定用户界面中的初始设置。

过滤器名称和属性提供了构建用户界面所需的所有信息,以允许用户选择过滤器并控制其输入参数。 过滤器的属性告诉您过滤器有多少个输入参数,参数名称,数据类型以及最小值,最大值和默认值。

Note: If you are interested in building a user interface for a Core Image filter, see IKFilterUIView Class Reference, which provides a view that contains input parameter controls for a Core Image filter. 注意:如果您有兴趣为Core Image过滤器构建用户界面,请参阅 IKFilterUIView Class Reference,该参考提供了一个包含Core Image过滤器的输入参数控件的视图。

Listing 4-1显示了获取过滤器名称并通过功能类别构建过滤器字典的代码。 代码检索这些类别中的过滤器(kCICategoryGeometryAdjustment,kCICategoryDistortionEffect,kCICategorySharpen和kCICategoryBlur),但会根据App定义的功能类别(如Distortion和Focus)构建字典。 功能类别可用于在菜单中组织过滤器名称,这对用户来说是有意义的。 该代码不会遍历所有可能的Core Image过滤器类别,但您可以通过遵循相同的过程轻松地扩展此代码。

// Listing 4-1  Code that builds a dictionary of filters by functional categories

NSMutableDictionary *filtersByCategory = [NSMutableDictionary dictionary];
 
NSMutableArray *filterNames = [NSMutableArray array];
[filterNames addObjectsFromArray:
    [CIFilter filterNamesInCategory:kCICategoryGeometryAdjustment]];
[filterNames addObjectsFromArray:
    [CIFilter filterNamesInCategory:kCICategoryDistortionEffect]];
filtersByCategory[@"Distortion"] = [self buildFilterDictionary: filterNames];
 
[filterNames removeAllObjects];
[filterNames addObjectsFromArray:
    [CIFilter filterNamesInCategory:kCICategorySharpen]];
[filterNames addObjectsFromArray:
    [CIFilter filterNamesInCategory:kCICategoryBlur]];
filtersByCategory[@"Focus"] = [self buildFilterDictionary: filterNames];

Listing 4-2显示了Listing 4-1中调用的buildFilterDictionary例程。 此例程为功能类别中的每个过滤器构建一个属性字典。 列表中的每一行代码都有详细的解释。

// Listing 4-2  Building a dictionary of filters by functional name

- (NSMutableDictionary *)buildFilterDictionary:(NSArray *)filterClassNames  // 1
{
    NSMutableDictionary *filters = [NSMutableDictionary dictionary];
    for (NSString *className in filterClassNames) {                         // 2
        CIFilter *filter = [CIFilter filterWithName:className];             // 3
 
        if (filter) {
            filters[className] = [filter attributes];                       // 4
        } else {
            NSLog(@"could not create '%@' filter", className);
        }
    }
    return filters;
}

代码如下:

  • 将一组过滤器名称作为输入参数。 回忆一下 Listing 4-1,这个数组可以是来自多个Core Image过滤器类别的过滤器名称的连接。 在这个例子中,数组是基于App设置的功能类别(失真或焦点)。
  • 遍历过滤器名称数组。
  • 检索过滤器名称的过滤器对象。
  • 检索过滤器的属性字典并将其添加到由例程返回的字典中。

Note: Apps that run in OS X v10.5 and later can use the CIFilter Image Kit additions to provide a filter browser and a view for setting filter input parameters. See CIFilter Image Kit Additions and ImageKit Programming Guide. 注意:在OS X v10.5及更高版本中运行的App可以使用CIFilter Image Kit附加组件来提供过滤器浏览和用于设置过滤器输入参数的视图。 请参阅CIFilter Image Kit AdditionsImageKit Programming Guide

后记

本篇已结束,后面更精彩~~~

Core Image框架详细解析(八) —— 查询系统中的过滤器 Querying the System for Filters_第3张图片

你可能感兴趣的:(Core Image框架详细解析(八) —— 查询系统中的过滤器 Querying the System for Filters)