Quartz 2D Programming Guide (1)-Overview

Source: Internet
Author: User

Overview of Quartz 2D


Quartz 2D is a two-dimensional graphics drawing engine that supports both iOS and Mac OS x environments, and we can use Quartz 2D to implement many features, such as basic path drawing, transparency, profiling, drawing shadows, transparent layers, color management, anti-aliasing, PDF document generation, and PDF elements Access to it. When needed, the Quartz 2D can also be aided by the functionality of the graphics hardware. in Mac OS x, Quartz 2D can be used in combination with other graphic image technologies, such as core image, Core Video, OpenGL, QuickTime. For example, by using the Graphicsimportcreatecgimage function of QuickTime, you can create an image from a QuickTime graphics importer with quartz 2D. Similarly, the Ios,quartz 2D is able to use a variety of drawing work and animation techniques, such as Core Animation,opengl es,uikit classes.
The Page
Quartz 2D uses the painter model in the drawing (painter's models). In the painter model, each successive drawing operation is a paint layer (a layer of paint) placed on the canvas. We usually call this canvas a drawing on the page.page to overlay more drawings with additional drawing operations. The Image object on page can only be changed by overlaying more graphics. This model allows us to use small primitives to construct complex graphs. Figure 1-1 shows how the painter model works. You can see that the different drawing order produces the same effect.
Figure 1-1 the painter ' s model

The page can be a piece of paper (if the output device is a printer), or it can be a virtual paper (if the output device is a PDF file), or a bitmap image, depending on the graphics context that is actually used.
Drawing Destinations (drawing target): The Graphics Context
The graphics context is a data type ( CGContextRef),用于封装 quartz the information that draws an image to the output device. The device can be a PDF file, bitmap or a display window. The information in the graphics context includes graphical drawing parameters and device-related representations of the images in the page. All objects in the quartz are drawn into a graphics context. We can imagine the graphics context as a drawing target, as shown in 1-2, when drawing with quartz, all device-related features are included in the graphics context we use, in other words, We can simply assign a different graphics Context to the quartz drawing sequence, and we can draw the same image to different devices, and we don't need any device-related calculations, which are done by quartz for us.
Figure 1-2 Quartz Drawing Destinations

we can get the following graphics contexts:
A Bitmap Graphics context:A Bitmap Graphics context allows you to use RGB colors, CMYK colors, or grayscale bitmap (bitmap). A bitmap bitmap is a rectangular array (or raster) of pixels, and each pixel represents a point in the image. Bitmap images are also named sampled images. More details: Creating a Bitmap Graphics Context.
A PDF Graphics Context:A PDF Graphics Context allows you to create a PDF file in which your drawing will be stored as a series of instructions. Some of the important differences between PDF files and bitmaps (bitmap) are as follows:1):P DFA file is not like a bitmap, it can contain multiple pages.2):when you draw the page fromPDFdocuments to different devices, the resulting image will be optimized according to the characteristics of the device. 3):P DF document has its own unique resolution, without sacrificing the image details, the size can be as large as possible and reduced. The user is able to perceive the problem and the quality and resolution are relevant. More details: Creating a PDF Graphics Context.
A Window Graphics context: Allows you to draw to the windows, more details: Creating A window graphics context in Mac OS X A Layer Context ( CGLayerRef ):PostScript Graphics Context: When you want to draw to Mac OS X, you will make the content appear on the PostScript graphics context. More details: Obtaining a Graphics Context for Printing
Quartz 2D Opaque Data Types
In addition to the graphics contexts, the Quartz 2D API defines a number of data types that start with CG because these APIs are part of the core graphics framework. Quartz 2D uses these data types to create objects that manipulate these objects to obtain specific graphics, figure 1-3 lists three images and related instructions for drawing operations using Quartz 2D:
you can rotate and display a PDF pageyou can draw pattern objects using a certain pattern. you can use a certain amount of pixels to fill an area, or to achieve a rounded effect.
Figure 1-3 Opaque data types is the basis of drawing primitives in Quartz 2D
The following data types are included in the Quartz 2D:
To CGPathRef create a path for vector graphics and populate it. See Paths. CGImageRef to display bitmaps and bitmap masks based on the relevant data provided. See Bitmap Images and Image masks.To CGLayerRef display the drawn layer, and the layer can be drawn repeatedly (such as for backgrounds or patterns) and off-screen rendering. See Core Graphics Layer Drawing CGPatternRef , which is used for repeating drawing. See Patterns. CGShadingRef CGGradientRef and, for drawing gradients. See gradients. CGFunctionRef , which defines a callback function that contains a random floating-point number that uses this data type when creating a gradient shadow effect. See gradients. CGColorRef CGColorSpaceRef and, used to inform quartz to understand the corresponding color. See color and Color Spaces. CGImageSourceRef CGImageDestinationRef and, used to move the data out of the quartz. See Data Management in Quartz 2D and Image I/O programmingGuide. CGFontRef , which is used to draw text. See Text.,,,, CGPDFDictionaryRef CGPDFObjectRef CGPDFPageRef CGPDFStream CGPDFStringRef ,, and CGPDFArrayRef , provide PDF metadata. See PDF Document Creation, viewing, and transforming. CGPDFScannerRef CGPDFContentStreamRef and, parse the PDF metadata. See PDF Document parsing. CGPSConverterRef , used to convert PostScript to PDF and not available in iOS. See PostScript Conversion.
Graphics states (graphic state)
Quartz Modifies the result of the drawing based on the parameters that modify the current drawing state. The drawing program determines how the results are rendered based on these drawing states. For example: When you call a function that sets the fill color, you change the color value stored in the current drawing state. The graphics contexts contains a drawing state stack, and when quartz creates a graphics contexts, the stack is empty. When the image state is saved, quartz presses a copy of the current shape pattern into the stack. When the graphics state is restored, quartz the graph state of the top of the stack. The status of the out stack is the current drawing state. You can use the function cgcontextsavegstate to save the graphics state, cgcontextrestoregstate to restore the graphics state.
Note: Not all aspects of the current drawing environment are elements of the graphical state. Such as: The graphics state does not contain the current path. The following is a list of graph state-related parameters:

Quartz 2D coordinate Systems
The coordinate system definition is the position and size range of the object that is drawn to the page, as shown in 1-4. We specify the position and size of the graphic in the user space coordinate system (User-space coordination System, abbreviated user space). Coordinate values are defined by floating-point numbers.
Figure 1-4 The Quartz coordinate system

because different devices have different graphics functions, the location and size of the images depend on the device. For example, a display device might show only less than 96 pixels per inch, while a printer might display 300 pixels per inch. If you define a coordinate system at the device level, graphics drawn on one device cannot be displayed properly on other devices. Quartz solves device dependencies by mapping a separate coordinate system (user space) to the coordinate system (device space) of the output device by using the current transformation matrix (transformation matrix, CTM). c5> problem . CTM is a special type of matrix (affine transform, affine matrix) that maps points from one coordinate space to another by panning (translation), rotation (rotation), scaling (scale) operations. CTM has another purpose: to allow you to determine how objects are drawn through transformations. For example, to draw a box that rotates 45 degrees, we can rotate the page's coordinate system before the box is drawn. The quartz uses a rotated coordinate system to draw the box to the output device.        the point of the user space is represented by a coordinate pair (x, y), and (0, 0) represents the origin of the coordinates. The default coordinate system in Quartz is: the value of the coordinates from left to right along the x-axis increases gradually, along the y-axis from the bottom to the upper coordinate. Some techniques use a different default coordinate system than quartz when setting up their graphics context. In contrast to quartz, these coordinate systems are modified coordinate systems (modified coordinate system), which must be converted when the graphics quartz drawn are displayed in these coordinate systems. The most common kind of modified coordinate system is that the origin is in the upper-left corner, and the value of the coordinate along the y-axis from top to bottom gradually increases. We can see this coordinate system in some of the following places:
in Mac OS x, override the Isflipped method to return the subclass of the NSView class for Yesin iOS, the drawing context returned by UIViewin iOS, the drawing context returned by calling the Uigraphicsbeginimagecontextwithoptions function
If the application wants to draw on a UIView object and a PDF graphics context with the same drawing program, a transform is needed to make the PDF graphics context use the same coordinate system as the UIView 。 To achieve this, you only need to translate the origin of the context of the PDF (move to the upper-left corner) and scale the y-coordinate value by 1. Figure 1-5 shows this transformation operation:



Our application is responsible for adjusting the quartz call to ensure that there is a transition applied to the context. For example, if you want a picture or pdf correctly drawn into a graphics context, your application may need to temporarily adjust the graphics context of the CTM. In iOS, if you use the UIImage object to wrap the created Cgimage object, you do not need to modify the CTM. The uiimage will automatically compensate for the Uikit coordinate system. Note: If you are developing programs related to quartz on iOS, it is necessary to know the above knowledge points, but not enough. In iOS3.2 and later versions, when Uikit created the drawing context for your application, the context was also modified to match the Uikit conventions. In particular, patterns and shadows are not affected by CTM and need to be individually adjusted to match the Uikit coordinate system. In this case, there is no equivalent mechanism for CTM to transform the quartz and uikit contexts. It is important to understand the context in which it is drawn, and it needs to match the expected context.
Memory Management: Object Ownership
Quartz uses the core Foundation memory management model (reference count). Therefore, the creation and destruction of objects is the same as the usual way. In quartz, here are some rules to keep in mind:If you create or copy an object, you will have it, so you must release it. In general, if you get an object using a function that contains a "Create" or "Copy" word, you must release it when you are done with it, or it will cause a memory leak. If you get an object using a function that does not contain a "Create" or "Copy" word, you will not have a reference to the object, and you do not need to release it. If you do not own an object and intend to keep it, you must retain it and release it when it is not needed. You can use the Quartz 2D function to specify retain and release an object. For example, if you create a Cgcolorspace object, use the functions Cgcolorspaceretain and cgcolorspacerelease to retain and release the object. Similarly, you can use the Cfretain and cfrelease of the core Foundation, but be aware that you cannot pass null values to these functions.
ReferenceQuartz 2D Programming Guide

Quartz 2D Programming Guide (1)-Overview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.