Chapter 14. Graphics

14.0. Introduction

The iOS SDK allows programmers to work with Quartz Core, which is encapsulated in the Core Graphics framework. In this chapter, we will use the Core Graphics framework extensively, so please make sure you have this framework added to your Xcode project by following these steps:

  1. Find the Frameworks item in your Xcode project hierarchy and right-click on it.

  2. Choose AddExisting Frameworks.

  3. Hold down the Command key and select the CoreGraphics.framework and QuartzCore.framework frameworks.

  4. Click the Add button.

In this chapter, we will be using functions and methods available in the Core Graphics and Quartz Core frameworks. Quartz 2D is the engine in the Core Graphics framework that allows us to draw sophisticated shapes, paths, images, and so on.

Make sure you import both frameworks into your source files whenever needed, like so:

#import <QuartzCore/QuartzCore.h>
#import <CoreGraphics/CoreGraphics.h>

Almost all the recipes in this chapter make use of the drawRect: method of an instance of UIView. This method gets called whenever a view has to be drawn. The only parameter to this method is of type CGRect, which tells you the rectangular area where you are supposed to be doing your painting. Please refrain from painting anything that is off this rectangle, as this will decrease the performance of your application.

To be able to use the code samples in these recipes, make sure you subclass a UIView object by following these steps:

  1. In Xcode, choose File

Get iOS 4 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.