Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning iOS5 Development.pdf
Скачиваний:
7
Добавлен:
09.05.2015
Размер:
15.6 Mб
Скачать

CHAPTER 16: Drawing with Quartz and OpenGL

569

To make matters even more confusing, there are different versions of some of these models, including several variants of the RGB color space.

Fortunately, for most operations, we don’t need to worry about the color model that is being used. We can just pass CGColor from our UIColor object, and in most cases, Core Graphics will handle any necessary conversions. If you use UIColor or CGColor when working with OpenGL ES, it’s important to keep in mind that they support other color models, because OpenGL ES requires colors to be specified in RGBA.

Color Convenience Methods

UIColor has a large number of convenience methods that return UIColor objects initialized to a specific color. In our previous code sample, we used the redColor method to initialize a color to red.

Fortunately, the UIColor instances created by most of these convenience methods all use the RGBA color model. The only exceptions are the predefined UIColors that represent grayscale values—such as blackColor, whiteColor, and darkGrayColor— which are defined only in terms of white level and alpha. In our examples here, we’re not using those, so we can assume RGBA for now.

If you need more control over color, instead of using one of those convenience methods based on the name of the color, you can create a color by specifying all four of the components. Here’s an example:

return [UIColor colorWithRed:1.0f green:0.0f blue:0.0f alpha:1.0f];

Drawing Images in Context

Quartz allows you to draw images directly into a context. This is another example of an Objective-C class (UIImage) that you can use as an alternative to working with a Core Graphics data structure (CGImage). The UIImage class contains methods to draw its image into the current context. You’ll need to identify where the image should appear in the context using either of the following techniques:

By specifying a CGPoint to identify the image’s upper-left corner

By specifying a CGRect to frame the image, resized to fit the frame, if necessary

You can draw a UIImage into the current context like so:

CGPoint drawPoint = CGPointMake(100.0f, 100.0f); [image drawAtPoint:drawPoint];

Drawing Shapes: Polygons, Lines, and Curves

Quartz provides a number of functions to make it easier to create complex shapes. To draw a rectangle or a polygon, you don’t need to calculate angles, draw lines, or do any math at all. You can just call a Quartz function to do the work for you. For example, to

www.it-ebooks.info

570

CHAPTER 16: Drawing with Quartz and OpenGL

draw an ellipse, you define the rectangle into which the ellipse needs to fit and let Core Graphics do the work:

CGRect theRect = CGMakeRect(0,0,100,100);

CGContextAddEllipseInRect(context, theRect);

CGContextDrawPath(context, kCGPathFillStroke);

You use similar methods for rectangles. Quartz also provides methods that let you create more complex shapes, such as arcs and Bezier paths.

NOTE: We won’t be working with complex shapes in this chapter’s examples. To learn more about arcs and Bezier paths in Quartz, check out the Quartz 2D Programming Guide in the iOS

Dev Center at

http://developer.apple.com/documentation/GraphicsImaging/Conceptua

l/drawingwithquartz2d/ or in Xcode’s online documentation.

Quartz 2D Tool Sampler: Patterns, Gradients, and Dash

Patterns

Although not as expansive as OpenGL ES, Quartz does offer quite an impressive array of tools. For example, Quartz supports filling polygons with gradients, not just solid colors, and an assortment of dash patterns in addition to solid lines. Take a look at the screenshots in Figure 16–4, which are from Apple’s QuartzDemo sample code, to see a sampling of what Quartz can do for you.

www.it-ebooks.info

CHAPTER 16: Drawing with Quartz and OpenGL

571

Figure 16–4. Some examples of what Quartz 2D can do, from the QuartzDemo sample project provided by Apple

Now that you have a basic understanding of how Quartz works and what it is capable of doing, let’s try it out.

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]