iOS Drawing: Learning Contexts and Basic UIKit Fundamentals
Drawing contexts are the virtual canvases you draw into from your applications. In this chapter, you review core technologies that underlie iOS drawing. You dive into contexts, discovering how to build and draw into them. You use contexts to create images, documents, and custom views, learning about basic drawing fundamentals in UIKit, Core Graphics, and Quartz.
iOS’s drawing routines are primarily sourced from the UIKit and QuartzCore frameworks. They consist of a mix of modern Objective-C interfaces (from UIKit) along with older C-based functions and Core Foundation-style objects (from QuartzCore). These items live side by side in your code. Here’s an example that shows a UIKit drawing operation followed by a Quartz one:
// Draw a rounded rectangle in UIKit UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:inset cornerRadius:12]; [bezierPath stroke]; // Fill an ellipse in Quartz CGContextFillEllipseInRect(context, rect);
The QuartzCore framework is commonly referred to as Quartz or Quartz 2D. The latter is Apple’s official name for its native 2D rendering and antialiasing API. It’s also the name used for the primary drawing reference in Apple’s developer library, the “Quartz 2D Programming Guide.”
The reach of this framework is wider than the Quartz 2D name might suggest. The QuartzCore framework implements compositing as well as rendering, along with many other graphics features. For example, the QuartzCore APIs include animation, tiling, and Core Image filtering (even though Core Image is properly its own framework).
To iOS developers, Quartz is often better known by its internal implementation name, Core Graphics. This book uses the terms Quartz and Core Graphics synonymously throughout. Most of the C-based APIs start with the two-letter CG prefix, derived from the Core Graphics name. From geometry (CGRect and CGPoint) to objects (CGColorRef, CGImageRef, and CGDataProviderRef), Quartz offers a vast, rich trove of drawing technology.
Quartz and its predecessors have been around for a long time. Contributing elements date back to the 1980s, when Display PostScript powered the graphics on the NeXTStep operating system. Quartz still uses an internal imaging model that’s very similar to modern PDF.
With each iOS release, updates continue to move the drawing APIs further toward Objective-C, simplifying, enhancing, and refining programmatic drawing. Core Graphics functions, however, continue to play an important role in day-to-day drawing development. Although newer routines replace many common tasks, they haven’t supplanted them all. Be prepared to work with both frameworks for the foreseeable future.