Drawing with Quartz and UIKit IPHONE APPS

Quartz is the general name for the native window server and drawing technology in iPhone OS. The Core Graphics framework is at the heart of Quartz, and is the primary interface you use for drawing content. This framework provides data types and functions for manipulating the following:

  • Graphics contexts
  • Paths
  • Images and bitmaps
  • Transparency layers
  • Colors, pattern colors, and color spaces
  • Gradients and shadings
  • Fonts
  • PDF content

UIKit builds on the basic features of Quartz by providing a focused set of classes for graphics related operations. The UIKit graphics classes are not intended as a comprehensive set of drawing tools—Core Graphics already provides that. Instead, they provide drawing support for other UIKit classes. UIKit support includes the following classes and functions:

  • UIImage, which implements an immutable class for displaying images
  • UIColor, which provides basic support for device colors
  • UIFont, which provides font information for classes that need it
  • UIScreen, which provides basic information about the screen
  • Functions for generating a JPEG or PNG representation of a UIImage object
  • Functions for drawing rectangles and clipping the drawing area
  • Functions for changing and getting the current graphics context

Configuring the Graphics Context

By the time your drawRect: method is called, your view’s built-in drawing code has already created and configured a default graphics context for you. You can retrieve a pointer to this graphics context by calling the UIGraphicsGetCurrentContext function. This function returns a reference to a CGContextRef type, which you pass to Core Graphics functions to modify the current graphics state. Table lists the main functions you use to set different aspects of the graphics state. This table also lists UIKit alternatives where they exist.

Core graphics functions for modifying graphics state

Core graphics functions for modifying graphics stateCore graphics functions for modifying graphics state

The graphics context contains a stack of saved graphics states. When Quartz creates a graphics context, the stack is empty. Using the CG Context Save GState function pushes a copy of the current graphics state onto the stack. Thereafter, modifications you make to the graphics state affect subsequent drawing operations but do not affect the copy stored on the stack. When you are done making modifications, you can return to the previous graphics state by popping the saved state off the top of the stack using the CG Context Restore GState function. Pushing and popping graphics states in this manner is a fast way to return to a previous state and eliminates the need to undo each state change individually. It is also the only way to restore some aspects of the state, such as the clipping path, back to their original settings.

Creating and Drawing Images

iPhone OS provides support for loading and displaying images using both the UIKit and Core Graphics frameworks. How you determine which classes and functions to use to draw images depends on how you intend to use them. Whenever possible, though, it is recommended that you use the classes of UIKit for representing images in your code. Table lists some of the usage scenarios and the recommended options for handling them.

Usage scenarios for images

Usage scenarios for images

The following example shows how to load an image from your application’s bundle. You can subsequently use this image object to initialize a UIImageView object, or you can store it and draw it explicitly in your view’s drawRect: method.

To draw an image explicitly in your view’s drawRect: method, you can use any of the drawing methods available in UIImage.These methods let you specify where in your view you want to draw the image and therefore do not require you to create and apply a separate transform prior to drawing. Assuming you stored the previously loaded image in a member variable called anImage, the following example draws that image at the point (10, 10) in the view.

Creating and Drawing Paths

A path is a description of a 2D geometric scene that uses a sequence of lines and Bézier curves to represent that scene. UIKit includes the UI RectFrame and UI RectFill functions (among others) for drawing simple paths such as rectangles in your views. Core Graphics also includes convenience functions for creating simple paths such as rectangles andellipses. For more complex paths, you must create the path yourself using the functions of the Core Graphics framework.

To create a path, you use the CG Context Begin Path function to configure the graphics context to receive path commands. After calling that function, you use other path-related functions to set the path’s starting point, draw lines and curves, add rectangles and ellipses, and so on. When you are done specifying the path geometry, you can paint the path directly or create a CG Path Ref or CG Mutable Path Ref data type to store a reference to that path for later use.

When you want to draw a path in your view, you can stroke it, fill it, or do both. Stroking a path with a function such as CGContextStrokePath creates a line centered on the path using the current stroke color. Filling the path with the CG Context FillPath function uses the current fill color or fill pattern to fill the area enclosed by the path’s line segments.

For more information on how to draw paths, including information about how you specify the points for complex path elements, see Paths in Quartz 2D Programming Guide. For information on the functions you use to create paths, see CG Context Reference and CG Path Reference.

Creating Patterns, Gradients, and Shadings

The Core Graphics framework includes additional functions for creating patterns, gradients, and shadings. You use these types to create non monochrome colors and use them to fill the paths you create. Patterns are created from repeating images or content. Gradients and shadings provide different ways to create smooth transitions from color to color.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status