美文网首页
Quartz 2D 编程指南十一:位图和图像遮罩(1)

Quartz 2D 编程指南十一:位图和图像遮罩(1)

作者: bobociel | 来源:发表于2017-08-14 17:46 被阅读63次

Bitmap Images and Image Masks 位图和图片遮照

Bitmap images and image masks are like any drawing primitive in Quartz. Both images and image masks in Quartz are represented by the CGImageRef data type. As you’ll see later in this chapter, there are a variety of functions that you can use to create an image. Some of them require a data provider or an image source to supply bitmap data. Other functions create an image from an existing image either by copying the image or by applying an operation to the image. No matter how you create a bitmap image in Quartz, you can draw the image to any flavor of graphics context. Keep in mind that a bitmap image is an array of bits at a specific resolution. If you draw a bitmap image to a resolution-independent graphics context (such as a PDF graphics context) the bitmap is limited by the resolution at which you created it.

位图图像和图像遮罩就像Quartz中的其他绘制元素一样。这两者都是用CGInageRef数据类型表示。 正如本章稍后讲的,我们可以使用一系列函数来创建图像。 其中一些需要数据提供者或图像源来提供位图数据。 另一些函数通过复制图像或对图像应用操作从现有图像创建图像。 不管我们用何种方式来创建图像,我们都可以将图像绘制到任何类型的图形上下文。 请记住,位图图像是特定分辨率的字节数组。 如果将位图图像绘制到依赖于分辨率的图形上下文(例如PDF图形上下文),则位图将受到该图形上下文分辨率的影响。

There is one way to create a Quartz image mask—by calling the function CGImageMaskCreate. You’ll see how to create one in Creating an Image Mask. Applying an image mask is not the only way to mask drawing. The sections Masking an Image with Color, Masking an Image with an Image Mask, and Masking an Image by Clipping the Context discuss all the masking methods available in Quartz.

我们可以通过调用CGImageMaskCreate函数来创建一个Quartz图像遮罩。我们将在“创建图像遮照”章节看到如何创建遮罩。使用图像遮罩不是绘制遮罩的唯一方法,具体的我们会在下面看到。

About Bitmap Images and Image Masks 位图和图像遮罩

A bitmap image (or sampled image) is an array of pixels (or samples). Each pixel represents a single point in the image. JPEG, TIFF, and PNG graphics files are examples of bitmap images. Application icons are bitmap images. Bitmap images are restricted to rectangular shapes. But with the use of the alpha component, they can appear to take on a variety of shapes and can be rotated and clipped, as shown in Figure 11-1.

一个位图是像素数组。每个像素表示图像中的一个点。JPEG,TUFF和PNG图像文件都是位图。应用程序的icon也是位图。位图被限定在一个矩形内。但是通过使用alpha分量,它们可以呈现不同的形式,也可以旋转或被裁剪,如图11-1所示.

Figure 11-1 Bitmap images

16- and 32-bit pixel formats for CMYK and RGB color spaces in Quartz 2D

Creating Images 创建图像

Table 11-1 lists the functions that Quartz provides to create CGImage objects. The choice of image creation function depends on the source of the image data. The most flexible function is CGImageCreate. It creates an image from any kind of bitmap data. However, it’s the most complex function to use because you must specify all bitmap information. To use this function, you need to be familiar with the topics discussed in Bitmap Image Information.

表11-1列出了Quartz提供的用于创建CGImageRef对象的函数。函数的选择依赖于图像的数据源。最常用的函数是CGImageCreate。它可以从任何类型的位图数据来创建图像。然而,它是最复杂的函数,因为需要提供所有的位图信息。为了使用这个函数,我们需要熟悉上面讨论的位图图像信息内容。

If you want to create a CGImage object from an image file that uses a standard image format such as PNG or JPEG, the easiest solution is to call the function CGImageSourceCreateWithURL to create an image source and then call the function CGImageSourceCreateImageAtIndex to create an image from the image data at a specific index in the image source. If the original image file contains only one image, then provide 0 as the index. If the image file format supports files that contain multiple images, you need to supply the index to the appropriate image, keeping in mind that the index values start at 0.

如果我们想从一个标准的图像格式,如PNG或JPEG,来创建一个CGImage对象,则最简单的方法就是调用函数CGImageSourceCreateWithURL 来创建一个图像源,然后调用CGImageSourceCreateImageAtIndex以使用从图像源中索引index指定的图像数据来创建一个图像。如果图像文件只包含一个图像,则索引为0。如果图像文件格式支持包含多个图像的文件,则需要提供所需要图像的索引值,确保索引的起始值为0。

If you’ve drawn content to a bitmap graphics context and want to capture that drawing to a CGImage object, call the function CGBitmapContextCreateImage.

如果我们已经将内容渲染到一个位图图像上下文,并想要从中获取CGImage对象,则可以调用CGBitmapContextCreateImage函数。

Several functions are utilities that operate on existing images, either to make a copy, create a thumbnail, or create an image from a portion of a larger one. Regardless of how you create a CGImage object, you use the function CGContextDrawImage to draw the image to a graphics context. Keep in mind that CGImage objects are immutable. When you no longer need a CGImage object, release it by calling the function CGImageRelease.

有几个函数可以操作已有的图像,如拷贝,创建缩略图,从一个大图像中创建一个图像。不管如何创建图像对象,我们都使用函数CGContextDrawImage将图像绘制到一个图像上下文中。记住CGImage是不可变的。当不再需要一个CGImage对象时,使用CGImageRelease函数释放它。

Table 11-1 Functions for creating images

Function Description
CGImageCreate A flexible function for creating an image. You must specify all the bitmap information that is discussed in Bitmap Image Information.
CGImageSourceCreateImageAtIndex Creates an image from an image source. Image sources can contain more than one image. See Data Management in Quartz 2D for information on creating an image source.
CGImageSourceCreateThumbnailAtIndex Creates a thumbnail image of an image that is associated with an image source. See Data Management in Quartz 2D for information on creating an image source.
CGBitmapContextCreateImage Creates an image by copying the bits from a bitmap graphics context.
CGImageCreateWithImageInRect Creates an image from the data contained within a sub-rectangle of an image.
CGImageCreateCopy A utility function that creates a copy of an image.
CGImageCreateCopyWithColorSpace A utility function that creates a copy of an image and replaces its color space.

The sections that follow discuss how to create:

  • A subimage from an existing image
  • An image from a bitmap graphics context

接下来我们讨论如何创建:

  • 从已存在图像中获取子图像
  • 从图形上下文中获取图像

You can consult these sources for additional information:

相关文章

网友评论

      本文标题:Quartz 2D 编程指南十一:位图和图像遮罩(1)

      本文链接:https://www.haomeiwen.com/subject/liqlfxtx.html