Data Management in Quartz 2D
Managing data is a task every graphics application needs to perform. For Quartz, data management refers to supplying data to or receiving data from Quartz 2D routines. Some Quartz 2D routines move data into Quartz, such as those that get image or PDF data from a file or another part of your application. Other routines accept Quartz data, such as those that write image or PDF data to a file or provide the data to another part of your application.
管理数据是每个图形应用程序需要执行的任务。 对于Quartz,数据管理是指向Quartz 2D提供数据或接收数据。 一些Quartz 2D程序将数据移动到Quartz中,例如那些从文件或应用程序的另一部分获取图像或PDF数据的程序。 其他例程接受Quartz数据,例如将图像或PDF数据写入文件或将数据提供给应用程序的其他部分的数据。
Quartz provides a variety of functions for managing data. By reading this chapter, you should be able to determine which functions are best for your application.
Quartz提供了各种管理数据的功能。 通过阅读本章,您应该可以确定哪些功能最适合您的应用。
Note: The preferred way to read and write image data is to use the Image I/O framework, which is available in iOS 4 and Mac OS X 10.4 and later. See Image I/O Programming Guide for more information on the CGImageSourceRef and CGImageDestinationRef opaque data types. Image sources and destinations not only offer access to image data, but also provide better support for accessing image metadata.
注意:读取和写入图像数据的首选方法是使用可在iOS 4和Mac OS X 10.4及更高版本中使用的Image I / O框架。有关CGImageSourceRef和CGImageDestinationRef不透明数据类型的更多信息,请参阅图像I / O编程指南。图像源和目的地不仅可以访问图像数据,还可以更好地支持访问图像元数据。
Quartz recognizes three broad categories of data sources and destinations:
- URL. Data whose location can be specified as a URL can act as a supplier or receiver of data. You pass a URL to a Quartz function using the Core Foundation data type CFURLRef.
- CFData. The Core Foundation data types CFDataRef
and CFMutableDataRef are data objects that let simple allocated buffers take on the behavior of Core Foundation objects. CFData is “toll-free bridged” with its Cocoa Foundation counterpart, theNSData class; if you are using Quartz 2D with the Cocoa framework, you can pass an NSData object to any Quartz function that takes a CFData object. - Raw data. You can provide a pointer to data of any type along with a set of callbacks that take care of basic memory management for the data.
Quartz可识别的三种类型数据源和目标:
- URL。可以将其位置指定为URL的数据作为数据的供应商或接收方。您使用Core Foundation数据类型CFURLRef将URL传递给Quartz函数。
- CFData。 Core Foundation数据类型为CFDataRef
而CFMutableDataRef是数据对象,它允许简单分配的缓冲区承担Core Foundation对象的行为。 CFData是与Cocoa Foundation对应的“免费桥接”,theNSData类;如果您使用Quartz 2D与Cocoa框架,则可以将-
NSData对象传递给接受CFData对象的任何Quartz函数。
原始数据。您可以提供指向任何类型的数据的指针,以及一组回调,该回调负责数据的基本内存管理。
The data itself, whether represented by a URL, a CFData object, or a data buffer, can be image data or PDF data. Image data can use any type of file format. Quartz understands most of the common image file formats. Some of the Quartz data management functions work specifically with image data, a few work only with PDF data, while others are more generic and can be used either for PDF or image data.
数据本身,无论是由URL,CFData对象还是数据缓冲区表示,都可以是图像数据或PDF数据。图像数据可以使用任何类型的文件格式。 Quartz能解析常见的图像文件格式。一些Quartz数据管理功能专门用于图像数据,一些仅使用PDF数据,而其他数据管理功能更通用,可用于PDF或图像数据。
URL, CFData, and raw data sources and destinations refer to data outside the realm of Mac OS X or iOS graphics technologies, as shown in Figure 10-1. Other graphics technologies in Mac OS X or iOS often provide their own routines to communicate with Quartz. For example, a Mac OS X application can send Quartz images to Core Image and use it to alter the image with sophisticated effects.
URL,CFData,原始数据源和目标数据是指Mac OS X或iOS图形技术领域之外的数据,如图10-1所示。 Mac OS X或iOS中的其他图形技术通常使用自己的方式与Quartz通信。例如,Mac OS X应用程序可以将Quartz图像发送到Core Image,并使用它来改变具有复杂效果的图像。
Moving Data into Quartz 2D(传输数据给Quartz 2D)
The functions for getting data from a data source are listed in Table 10-1. All these functions, except for CGPDFDocumentCreateWithURL, either return an image source (CGImageSourceRef) or a data provider (CGDataProviderRef). Image sources and data providers abstract the data-access task and eliminate the need for applications to manage data through a raw memory buffer.
从数据源获取数据的功能如表10-1所示。 除了CGPDFDocumentCreateWithURL之外,所有这些功能都返回图像源(CGImageSourceRef)或数据提供者(CGDataProviderRef)。 图像源和数据提供者抽象数据访问任务,消除了通过原始内存缓冲区来管理数据的应用程序的需求。
Image sources are the preferred way to move image data into Quartz. An image source represents a wide variety of image data. An image source can contain more than one image, thumbnail images, and properties for each image and the image file. After you have a CGImageSourceRef, you can accomplish these tasks:
- Create images (CGImageRef) using the functions CGImageSourceCreateImageAtIndex, CGImageSourceCreateThumbnailAtIndex, or CGImageSourceCreateIncremental. A CGImageRef data type represents a single Quartz image.
- Add content to an image source using the functions CGImageSourceUpdateData or CGImageSourceUpdateDataProvider.
- Obtain information from an image source using the functions CGImageSourceGetCount, CGImageSourceCopyProperties, and CGImageSourceCopyTypeIdentifiers.
图像源是将图像数据移动到Quartz的首选方式。 图像源代表各种各样的图像数据。 图像源可以包含多个图像,缩略图图像和每个图像和图像文件的属性。 在你有一个CGImageSourceRef之后,你可以完成这些任务:
- 使用函数CGImageSourceCreateImageAtIndex,CGImageSourceCreateThumbnailAtIndex或CGImageSourceCreateIncremental创建图像(CGImageRef)。 CGImageRef数据类型表示单个Quartz图像。
- 使用函数CGImageSourceUpdateData或CGImageSourceUpdateDataProvider将内容添加到图像源。
- 使用函数CGImageSourceGetCount,CGImageSourceCopyProperties和CGImageSourceCopyTypeIdentifiers从图像源获取信息。
The function CGPDFDocumentCreateWithURL is a convenience function that creates a PDF document from the file located at the specified URL. Data providers are an older mechanism with more limited functionality. They can be used to obtain image or PDF data. You can supply a data provider to:
- An image creation function, such as CGImageCreate, CGImageCreateWithPNGDataProvider, or CGImageCreateWithJPEGDataProvider.
- The PDF document creation function CGPDFDocumentCreateWithProvider.
- The function CGImageSourceUpdateDataProvider to update an existing image source with new data.
函数CGPDFDocumentCreateWithURL是一个方便的功能,可以从位于指定URL的文件创建PDF文档。数据提供者是一个更老的机制,功能更有限。它们可用于获取图像或PDF数据。您可以提供数据提供者:
- 图像创建功能,如CGImageCreate,
CGImageCreateWithPNGDataProvider或CGImageCreateWithJPEGDataProvider。 - PDF文档创建函数CGPDFDocumentCreateWithProvider。
- 函数CGImageSourceUpdateDataProvider使用新数据更新现有的图像源。
For more information on images, see Bitmap Images and Image Masks.
有关图像的更多信息,请参阅位图图像和图像遮照。
Table 10-1 Functions that move data into Quartz 2D
Function | Use this function |
---|---|
CGImageSourceCreateWithDataProvider | To create an image source from a data provider. |
CGImageSourceCreateWithData | To create an image source from a CFData object. |
CGImageSourceCreateWithURL | To create an image source from a URL that specifies the location of image data. |
CGPDFDocumentCreateWithURL | To create a PDF document from data that resides at the specified URL. |
CGDataProviderCreateSequential | To read image or PDF data in a stream. You supply callbacks to handle the data. |
CGDataProviderCreateDirectAccess | To read image or PDF data in a block. You supply callbacks to handle the data. |
CGDataProviderCreateWithData | To read a buffer of image or PDF data supplied by your application. You provide a callback to release the memory you allocated for the data. |
CGDataProviderCreateWithURL | Whenever you can supply a URL that specifies the target for data access to image or PDF data. |
CGDataProviderCreateWithCFData | To read image or PDF data from a CFData object. |
Moving Data out of Quartz 2D(获得Quartz 2D数据)
The functions listed in Table 10-2 move data out of Quartz 2D. All these functions, except for CGPDFContextCreateWithURL, either return an image destination (CGImageDestinationRef) or a data consumer (CGDataConsumerRef). Image destination and data consumers abstract the data-writing task, letting Quartz take care of the details for you.
表10-2中列出的功能将数据从Quartz 2D中移出。 除了CGPDFContextCreateWithURL之外,所有这些功能都返回图像目标(CGImageDestinationRef)或数据使用者(CGDataConsumerRef)。 图像目标和data consumers抽象的数据写入任务,让Quartz来处理细节。
An image destination is the preferred way to move image data out of Quartz. Similar to image sources, an image destination can represent a variety of image data, from a single image to a destination that contains multiple images, thumbnail images, and properties for each image or for the image file. After you have a CGImageDestinationRef, you can accomplish these tasks:
- Add images (CGImageRef) to a destination using the functions CGImageDestinationAddImage or CGImageDestinationAddImageFromSource. ACGImageRef data type represents a single Quartz image.
- Set properties using the function CGImageDestinationSetProperties.
- Obtain information from an image destination using the functions CGImageDestinationCopyTypeIdentifiers or CGImageDestinationGetTypeID.
图像目标是将图像数据移出Quartz的首选方式。 类似于图像源,图像目标可以表示从单个图像到包含多个图像的缩略图图像,以及每个图像或图像文件的属性的各种图像数据。 在你有一个CGImageDestinationRef之后,你可以完成这些任务:
- 使用函数CGImageDestinationAddImage或CGImageDestinationAddImageFromSource将图像(CGImageRef)添加到目标。 ACGImageRef数据类型表示单个Quartz图像。
- 使用函数CGImageDestinationSetProperties设置属性。
- 使用函数CGImageDestinationCopyTypeIdentifiers或CGImageDestinationGetTypeID从图像目的地获取信息。
The function CGPDFContextCreateWithURL is a convenience function that writes PDF data to the location specified by a URL. Data consumers are an older mechanism with more limited functionality. They are used to write image or PDF data. You can supply a data consumer to:
- The PDF context creation function CGPDFContextCreate. This function returns a graphics context that records your drawing as a sequence of PDF drawing commands that are passed to the data consumer object.
- The function CGImageDestinationCreateWithDataConsumer to create an image destination from a data consumer.
函数CGPDFContextCreateWithURL是将PDF数据写入由URL指定的位置的便利函数。data consumer是更老的机制,功能更有限。它们用于写图像或PDF数据。您可以提供data consumer:
- PDF上下文创建函数CGPDFContextCreate。此函数返回一个图形上下文,将图形记录为传递给数据用户对象的一系列PDF绘图命令。
- 函数CGImageDestinationCreateWithDataConsumer从data consumer创建一个图像目标。
Note: For the best performance when working with raw image data, use the vImage framework. You can import image data to vImage from a CGImageRef reference with the vImageBuffer_InitWithCGImage function. For details, see Accelerate Release Notes.
注意:为了在使用原始图像数据时获得最佳性能,请使用vImage框架。您可以使用vImageBuffer_InitWithCGImage函数从CGImageRef引用中导入图像数据到vImage。有关详细信息,请参阅Accelerate Release Notes。
For more information on images, see Bitmap Images and Image Masks.
有关图像的更多信息,请参阅位图图像和图像遮照。
Table 10-2 Functions that move data out of Quartz 2D
Function | Use this function |
---|---|
CGImageDestinationCreateWithDataConsumer | To write image data to a data consumer. |
CGImageDestinationCreateWithData | To write image data to a CFData object. |
CGImageDestinationCreateWithURL | Whenever you can supply a URL that specifies where to write the image data. |
CGPDFContextCreateWithURL | Whenever you can supply a URL that specifies where to write PDF data. |
CGDataConsumerCreateWithURL | Whenever you can supply a URL that specifies where to write the image or PDF data. |
CGDataConsumerCreateWithCFData | To write image or PDF data to a CFData object. |
CGDataConsumerCreate | To write image or PDF data using callbacks you supply. |
Moving Data Between Quartz 2D and Core Image in Mac OS X(Mac OS X上Quartz 2D和Core Image之间的数据传输)
The Core Image framework is an Objective-C API provided in Mac OS X that supports image processing. Core Image lets you access built-in image filters for both video and still images and provides support for custom filters and near real-time processing. You can apply Core Image filters to Quartz 2D images. For example, you can use Core Image to correct color, distort the geometry of images, blur or sharpen images, and create a transition between images. Core Image also allows you to apply an iterative process to an image—one that feeds back the output of a filter operation to the input. To understand the capabilities of Core Image more fully, see Core Image Programming Guide.
Core Image框架是Mac OS X中提供的Objective-C API,支持图像处理。 Core Image可让您访问视频和静态图像的内置图像过滤器,并支持自定义过滤器和近乎实时的处理。 您可以将Core Image过滤器应用于Quartz 2D图像。 例如,您可以使用Core Image来校正颜色,扭曲图像的几何图形,模糊或锐化图像,并在图像之间创建转换。 Core Image还允许您将迭代过程应用于将过滤器操作的输出反馈到输入的映像。 要了解Core Image的功能,请参阅Core Image Programming Guide。
Core Image methods operate on images that are packaged as Core Image images, or CIImage objects. Core Image does not operate directly on Quartz images (CGImageRef data types). Quartz images must be converted to Core Image images before you apply a Core Image filter to the image.
Core Image方法作用于Core Image或CIImage对象。 Core Image不直接在Quartz图像(CGImageRef数据类型)上运行。 在将Core Image过滤器应用于图像之前,Quartz图像必须转换为Core Image图像。
The Quartz 2D API does not provide any functions that package Quartz images as Core Image images, but Core Image does. The following Core Image methods create a Core Image image from either a Quartz image or a Quartz layer (CGLayerRef). You can use them to move Quartz 2D data to Core Image.
Quartz 2D API不提供将Quartz图像转为Core Image图像的任何功能,但Core Image所提供的功能。以下Core Image方法从Quartz图像或Quartz图层(CGLayerRef)创建Core Image图像。您可以使用它们将Quartz 2D数据移动到Core Image。
- imageWithCGImage:
- imageWithCGImage:options:
- imageWithCGLayer:
- imageWithCGLayer:options:
The following Core Image methods return a Quartz image from a Core Image image. You can use them to move a processed image back into Quartz 2D:
以下Core Image方法从Core Image图像返回Quartz图像。您可以使用它们将已处理的图像移回到Quartz 2D中:
- createCGImage:fromRect:
- createCGLayerWithSize:info:
For a complete description of Core Image methods, see Core Image Reference Collection.
有关Core Image方法的完整说明,请参阅Core Image Reference Collection