This chapter introduces the basis of iOS drawing: the context. You saw how to create contexts, read about the different kinds of contexts available, and discovered how to retrieve content for use in images and PDF files. In iOS, contexts offer a wide array of nuanced state control. Before you leave this chapter, here are a few points to think about:
- For any drawing requirement, there are usually many solutions that get you to the same place. Your comfort in using Core Graphics functions versus UIKit classes may influence your choice of APIs, but there’s often no right answer in how you must or should draw. Do whatever works best for your development needs.
UIKit classes continue evolving. With each generation of iOS, they provide more and more resources that enable you to skip direct Quartz. UIKit APIs offer the advantages of simplicity, parsimony, and ease of inspection. Most iOS developers will find that these advantages create a powerful case for primarily using UIKit.
Apple generally advises developers to work with the highest-level API possible. Use lower-level APIs when you need functionality that higher-level versions do not provide. There are numerous things you can do in the older-style APIs that you simply cannot do in UIKit, such as painting with calculated shadows or working with output color spaces.
Being comfortable using Core Foundation-style C with manual memory management continues to be a rewarding skill. Knowing how the Ref style objects work and when and how to release remains relevant for the foreseeable future. Learn to use ARC-style bridging to transfer memory management to and from the Core Foundation system. You’ll be glad you invested the time because of the greater flexibility you gain.
Remember this rule of thumb: If a function’s name says Copy or Create, you have to release items. If it says Get, you don’t. And, wherever possible, always look for a release function with the same prefix as the Create function. For example, if you create a color space, use CGColorSpaceRelease() to balance that.
- Both Quartz and UIKit drawing are thread-safe. Apple Technical Q&A 1637 notes that beginning with iOS 4.0, “drawing to a graphics context in UIKit is thread-safe. This includes accessing and manipulating the current graphics stack, drawing images and strings, and usage of color and font objects from secondary threads.”