Chapter 15. Drawing

Many UIView subclasses, such as a UIButton or a UITextField, know how to draw themselves; sooner or later, though, you’re going to want to do some drawing of your own. A class like UIImageView will display a static image; you can generate that image dynamically by drawing it in code. And a pure UIView does little or no drawing of its own; you can draw its appearance.

Drawing is not difficult, but it is a very large topic. There are some UIKit convenience methods, but the full API is provided by Core Graphics, often referred to as Quartz, or Quartz 2D. Core Graphics is the drawing system that underlies all iOS drawing — UIKit drawing is built on top of it — so it is low-level and consists of C functions, but it isn’t entirely alien. This chapter will familiarize you with the fundamentals. For complete information, you’ll want to study Apple’s Quartz 2D Programming Guide.

UIImageView draws an image for you and takes care of all the details. If you want to do any drawing for yourself, however, you must do so in a graphics context. A graphics context is basically a place you can draw. In certain situations, a graphics context is provided for you; otherwise, you must obtain or create one for yourself. Either way, this graphics context may also become the current context. Core Graphics drawing functions require that you specify a context to draw into; UIKit’s Objective-C drawing methods typically draw into the current context. If you have a context that you want to draw ...

Get Programming iOS 4 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.