Quartz 2D programming guide (1)-Overview

Source: Internet
Author: User

Quartz 2D programming guide is a series of quartz 2D learning materials translated by Druid, a Forum member for your reference.

Quartz 2D is a two-dimensional Graphics Rendering Engine that supports iOS and Mac OS X environments. We can use the quartz 2D API to implement many functions, such as basic path rendering, transparency, shadow, shadow rendering, transparent layer, color management, anti-sawtooth, PDF document generation and PDF metadata access. When necessary, quartz 2D can also use graphical hardware features.
In Mac OS X, quartz 2D can be used together with other graphics and image technologies, such as core image, core video, OpenGL, and QuickTime. For example, by using the graphicsimportcreatecgimage function of QuickTime, you can use quartz to create an image from a QuickTime graphics importer.

Page
Quartz 2D uses painter's model in the image ). In the painter model, each continuous Painting Operation places a painting layer (a layer of 'paint') on a canvas ('canvas '), we usually call this canvas (page ). The drawing on the page can be superimposed by additional drawing operations. The graphic objects on the page can only be changed by adding more drawings. This model allows us to use small elements to build complex graphics.
Figure 1-1 shows how the painter model works. We can see that the effects of different draw sequence are different.

 
A page can be a piece of paper (if the output device is a printer), a virtual paper (if the output device is a PDF file), or a bitmap image. This depends on the actual graphics context.

Drawing target: graphics context
Graphics context is a data type (cgcontextref) used to encapsulate information that quartz draws an image to the output device. The device can be a PDF file, bitmap, or display window. The information in graphics context includes the graphic rendering parameters of the image on the page and the representation of the device. All objects in quartz are drawn to a graphics context.
We can think of graphics context as the 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 specify different graphics context for the quartz plot sequence to draw the same image to different devices. We do not need any device-related computing; these are done by quartz for us.

 
Quartz provides the following graphics context types.

  • Bitmap graphics context
  • PDF graphics context
  • Window graphics context
  • Layer Context
  • Post graphics context


Quartz 2D Data Type
In addition to graphics context, the quartz 2D API also defines some data types. Because these APIs are part of the core graphics framework, these data types all start with CG.
Quartz 2D uses these data types to create objects and manipulate these objects to obtain specific images. Figure 1-3 illustrates three images obtained using quartz 2D plotting.

 
The following lists the Data Types contained in quartz 2D:

  • Cgpathref: used for vector graphs. You can create paths and fill or draw them (stroke)
  • Cgimageref: used to represent bitmap images and bitmap image masks Based on sampled data.
  • Cglayerref: used to indicate the painting layers that can be used for repeated painting (such as background) and offline painting.
  • Cgpatternref: used for redrawing
  • Cgshadingref and cggradientref: used to draw a gradient.
  • Cgfunctionref: used to define a callback function, which contains a random floating point value parameter. This type is used when creating a gradient for a shadow.
  • Cgcolorref, cgcolorspaceref: Used to tell quartz how to explain the color
  • Cgimagesourceref, cgimagedestinationref: used to move data in and out of quartz
  • Cgfontref: used to draw text
  • Cgpdfdictionaryref, cgpdfobjectref, cgpdfpageref, cg1_stream, cg1_stringref, and cgpdfarrayref: the metadata used to access PDF files.
  • Cgw.scannerref, cgw.contentstreamref: Used to parse PDF metadata
  • Cgpsconverterref: used to convert postscript to PDF. It cannot be used in IOS.


Graphics status
Quartz modifies the painting result by modifying the current graphics state. The graphic status contains parameters used to draw programs. The rendering program determines how to render the result based on the drawing status. For example, when you call the fill color function, you will change the color value stored in the current drawing status.
Graphics context contains a drawing state stack. When quartz creates a graphics context, the stack is empty. When the graph status is saved, quartz pushes a copy of the current graph status to the stack. When the graph state is restored, quartz outputs the graph state at the top of the stack. The status of the stack is the current graph status.
You can use the cgcontextsavegstate function to save the graph state, while cgcontextrestoregstate to restore the graph state.
Note: not all aspects of the current drawing environment are graphical state elements. For example, the image status does not contain the current path ). Parameters related to the graphic status are listed below:

  • Current transformation matrix (CTM): current conversion matrix
  • Clipping area: Cropping Area
  • Line: Line
  • Accuracy of curve estimation (flatness): Curve Smoothness
  • Anti-aliasing setting: Anti-sawtooth setting
  • Color: Color
  • Alpha value (transparency): Transparency
  • Rendering Intent: rendering target
  • Color Space: Color Space
  • Text: Text
  • Blend mode: Mixed Mode


Quartz 2D Coordinate System
The coordinate system defines the location and size range of the object drawn to the page, as shown in figure 1-4. In the user space coordination system (user space), we specify the position and size of the image. The coordinate value is defined by a floating point number.

 
Because different devices have different graphical functions, the image position and size depend on the device. For example, a display device may display at least 96 pixels per inch, while a printer may display at least 300 pixels per inch. If a coordinate system is defined at the device level, the images drawn on one device cannot be normally displayed on other devices.
Quartz maps an independent coordinate system (user space) to the device space of the output device by using the current transformation matrix (CTM ), to solve the device dependency problem. CTM is a special type of matrix (affine transform, affine matrix). It uses translation, rotation, and scale) the operation maps a point from a coordinate space to another coordinate space.
CTM has another purpose: to allow you to determine how an object is drawn through conversion. For example, to plot a box with 45 degrees of rotation, we can rotate the page coordinate system before the box is drawn. Quartz uses the rotated coordinate system to draw the box to the output device.
The user space points are represented by coordinate pairs (x, y), and (0, 0) represent the coordinate origin. In quartz, the default coordinate system is: increasing from left to right along the X axis, and increasing from bottom to top along the Y axis.
Some techniques use default coordinate systems different from those of quartz when setting their graphics context. Compared with quartz, these coordinate systems are modified coordinate systems, which must be converted when the Z plot is displayed in these coordinate systems. The most common modification of the coordinate system is that the origin is located in the upper left corner, and the coordinate value increases gradually along the Y axis from top to bottom. We can see this coordinate system in the following places:

  • In Mac OS X, the isflipped method is rewritten to return a subclass of the Yes nsview class.
  • In iOS, the drawing context returned by uiview
  • In iOS, call the drawing context returned by the uigraphicsbeginimagecontextwitexceptions Function

If the application wants to draw on a uiview object and a PDF graphics context using the same drawing program, a transformation is required so that the PDF graphics context uses the same coordinate system as the uiview. To achieve this goal, you only need to perform a translation (move to the upper left corner) on the source of the PDF context and scale the Y coordinate value with-1. Figure 1-5 shows the Transformation Operation:

 

Our application is responsible for adjusting the quartz call to ensure that there is a conversion application to the context. For example, if you want an image or PDF to be correctly drawn to a graphics context, your application may need to temporarily adjust the CTM of graphics context. In iOS, if you use a uiimage object to wrap the created cgimage object, you do not need to modify the CTM. Uiimage will be automatically compensated to apply to the coordinate system of uikit.

Copy code
  1. Important: If you plan to develop quartz-related programs on iOS, it is useful to understand the above, but not necessary. In IOS 3.2 and later versions, when uikit creates a drawing context for your application, it also makes additional modifications to the context to match the uikit conventions. In particular, patterns and shadows (not affected by CTM) are adjusted separately to match the uikit coordinate system. In this case, there is no equivalent mechanism for CTM to convert the context of quartz and uikit. We must recognize the context in which to draw and adjust the behavior to match the context expectation.



Memory Management: object ownership
Quartz uses the core foundation memory management model (reference count ). Therefore, the object creation and destruction are the same as the normal method. In quartz, remember the following rules:

  • If you create or copy an object, you will have it, so you must release it. Generally, if you use a function that contains the word "CREATE" or "copy" to obtain an object, the object must be released after use; otherwise, memory leakage will occur.
  • If you use a function that does not contain the word "CREATE" or "copy" to obtain an object, you do not have reference to the object and do not need to release it.
  • If you don't have an object and want to keep it, you must retain it and release it when you don't need it. You can use the quartz 2D function to specify the retain and release objects. For example, if a cgcolorspace object is created, the cgcolorspaceretain and cgcolorspacerelease functions are used to retain and release objects. Similarly, you can use cfretain and cfrelease of core foundation, but note that null values cannot be passed to these functions.

Original post address: http://www.cocoachina.com/bbs/read.php? Tid = 77731 & page = 2

Http://www.cocoachina.com/newbie/basic/2011/1111/3486.html

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.