Preface - iOS Drawing: Practical UIKit Solutions (2014)

iOS Drawing: Practical UIKit Solutions (2014)


Apple has lavished iOS with a rich and evolving library of 2D drawing utilities. These APIs include powerful features like transparency, path-based drawing, antialiasing, and more. They offer low-level, lightweight, easy-to-integrate drawing routines that you can harness to build images, to display views, and to print.

Importantly, these APIs are resolution independent. With careful thought, your drawing code can automatically adapt to new geometries and screen resolutions. What you draw fits itself to each device and its screen scale, allowing you to focus on the drawing content, not on the specifics of pixel implementations.

This book introduces Quartz and UIKit drawing. From basic painting to advanced Photoshop-like effects, you’ll discover a wealth of techniques you can use in your day-to-day development tasks. It’s written for iOS 7, so if you’re looking for material that reflects iOS’s newest incarnation, it’s here.

Ready to get started? The power you need is already in iOS. This book helps you unlock that power and bring it into your apps.

—Erica Sadun, October 2013

How This Book Is Organized

This book offers practical drawing how-to for iOS development. Here’s a rundown of what you find in this book’s chapters:

Image Chapter 1, “Drawing Contexts—This chapter introduces the virtual canvases you draw into from your applications. It reviews the core technologies that underlie drawing. Then it dives into contexts themselves, explaining how to build and draw into them and use their content to create images, documents, and custom views. By the time you finish this chapter, you’ll have explored the core of iOS drawing.

Image Chapter 2, “The Language of Geometry—Drawing and geometry are inextricably linked. In order to express a drawing operation to the compiler, you must describe it with geometric descriptions that iOS can interpret on your behalf. This chapter reviews basics you’ll need to get started. It begins with the point–pixel dichotomy, continues by diving into core structures, and then moves to UIKit objects. You’ll learn what these items are and the roles they play in drawing.

Image Chapter 3, “Drawing Images—This chapter surveys the techniques you need to create, adjust, and retrieve image instances. You’ll read about drawing into image instances, creating thumbnails, working with byte arrays, and more.

Image Chapter 4, “Path Basics—Paths are some of the most important tools for iOS drawing, enabling you to create and draw shapes, establish clipping paths, define animation paths, and more. Whether you’re building custom view elements, adding Photoshop-like special effects, or performing ordinary tasks like drawing lines and circles, a firm grounding in the UIBezierPath class will make your development easier and more powerful.

Image Chapter 5, “Paths in Depth—Exposing path internals ratchets up the way you work with the UIBezierPath class. This chapter explains how to leverage the CGPathElement data structures stored in each instance’s underlying CGPath to produce solutions for many common iOS drawing challenges. Want to place objects along a path’s curves? Want to divide a path into subpaths and color them individually? There are element-based solutions for these tasks, which you’ll discover in this chapter.

Image Chapter 6, “Drawing Gradients—In iOS, a gradient is a progression between colors. Gradients are used to shade drawings and simulate real-world lighting in computer-generated graphics. Gradients are an important component for many drawing tasks and can be leveraged for powerful visual effects. This chapter introduces iOS gradients and demonstrates how to use them to add UI pizzazz.

Image Chapter 7, “Masks, Blurs, and Animation—Masking, blurring, and animation are day-to-day development challenges you experience when drawing. This chapter introduces techniques that enable you to add soft edges, depth-of-field effects, and updates that change over time. This chapter surveys these technologies, introducing solutions for your iOS applications.

Image Chapter 8, “Drawing Text—The story of text drawing extends well beyond picking a point on a context and painting some text or transforming a string into a Bezier path. This chapter dives deep into text, covering advanced UIKit and Core Text techniques for drawing, measuring, and laying out strings.

Image Appendix A, “Blend Modes—This appendix offers a quick reference for Quartz’s important blend modes that enable you to blend new source material over an already-drawn destination.

Image Appendix B, “Miter Threshold Limits—You’ll read here about miter limits, the feature that automatically converts line joins into cropped, beveled results.

About the Sample Code

This book follows the trend I started in my iOS Developer Cookbooks. This book’s iOS sample code always starts off from a single main.m file, where you’ll find the heart of the application powering the example. This is not how people normally develop iOS or Cocoa applications or, honestly, how they should be developing them, but it’s a great way of presenting a single big idea. It’s hard to tell a story when readers must look through five or seven or nine files at once, trying to find out what is relevant and what is not. Offering a single file heart concentrates that story, allowing access to that idea in a single chunk.

The power of this book, however, lies in routines collected into the “Quartz Book Pack” in the sample code projects. These are standalone classes and functions intended to inspire use outside this book. I do recommend, however, that you add your own namespacing to avoid any potential conflict with future Apple updates. They include concise solutions that you can incorporate into your work as needed.

For the most part, the examples for this book use a single application identifier: com.sadun.hello-world. This avoids clogging up your iOS devices with dozens of examples at once. Each example replaces the previous one, ensuring that your home screen remains relatively uncluttered. If you want to install several examples simultaneously, simply edit the identifier by adding a unique suffix, such as com.sadun.helloworld.sample5.

You can also edit the custom display name to make the apps visually distinct. Your Team Provisioning Profile matches every application identifier, including com.sadun.helloworld. This allows you to install compiled code to devices without having to change the identifier; just make sure to update your signing identity in each project’s build settings.