Tuesday, 19 February 2013

Drawing with Quartz and UIKit | iPhone Apps Tutorial pdf

Drawing with Quartz and UIKit

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. For a complete list of functions, see CGContext  Reference. This table also lists UIKit alternatives where they exist.
Table: Core 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 CGContextSaveGState 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 CGContextRestoreGState 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.
For general information about graphics contexts and using them to configure the drawing environment, see Graphics Contexts in Quartz 2D Programming Guide.

=> 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 4-4 lists some of the usage scenarios and the recommended options for handling them.
Table:  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.
NSString* imagePath = [[NSBundle mainBundle] pathForResource:@"myImage"
ofType:@"png"];
UIImage* myImageObj = [[UIImage alloc] initWithContentsOfFile:imagePath];
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.
- (void)drawRect:(CGRect)rect
{
// Draw the image
[anImage drawAtPoint:CGPointMake(10, 10)];
}

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 UIRectFrame and UIRectFill 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 and ellipses. 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 CGContextBeginPath 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 CGPathRef or CGMutablePathRef 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 CGContextFillPath function uses the current fill color or fill pattern to fill the area
enclosed by the path’s line segments.

No comments: