CocoaDrawingGuide.pdf

(4457 KB) Pobierz
Cocoa Drawing Guide
Cocoa Drawing Guide
Graphics&Animation:2DDrawing
2011-01-18
695673697.002.png
Apple Inc.
© 2005, 2011 Apple Inc.
All rights reserved.
DEFECTORINACCURACYINTHISDOCUMENT,even
ifadvisedofthepossibilityofsuchdamages.
THEWARRANTYANDREMEDIESSETFORTHABOVE
AREEXCLUSIVEANDINLIEUOFALLOTHERS,ORAL
ORWRITTEN,EXPRESSORIMPLIED.NoApple
dealer,agent,oremployeeisauthorizedtomake
anymodification,extension,oradditiontothis
warranty.
Somestatesdonotallowtheexclusionorlimitation
ofimpliedwarrantiesorliabilityforincidentalor
consequentialdamages,sotheabovelimitationor
exclusionmaynotapplytoyou.Thiswarrantygives
youspecificlegalrights,andyoumayalsohave
otherrightswhichvaryfromstatetostate.
Nopartofthispublicationmaybereproduced,
stored in a retrieval system, or transmitted, in
any form or by any means, mechanical,
electronic, photocopying, recording, or
otherwise, without prior written permission of
Apple Inc., with the following exceptions: Any
person is hereby authorized to store
documentation on a single computer for
personal use only and to print copies of
documentation for personal use provided that
thedocumentationcontainsApple’scopyright
notice.
The Apple logo is a trademark of Apple Inc.
No licenses, express or implied, are granted
withrespecttoanyofthetechnologydescribed
in this document. Apple retains all intellectual
propertyrightsassociatedwiththetechnology
described in this document. This document is
intended to assist application developers to
develop applications only for Apple-labeled
computers.
Apple Inc.
1 Infinite Loop
Cupertino, CA 95014
408-996-1010
Apple, the Apple logo, Carbon, Cocoa,
ColorSync, Leopard, Mac, Mac OS, Macintosh,
Objective-C, Quartz, QuickDraw, QuickTime,
Snow Leopard, Spaces, and Xcode are
trademarks of Apple Inc., registered in the
United States and other countries.
Adobe,Acrobat,andPostScriptaretrademarks
or registered trademarks of Adobe Systems
IncorporatedintheU.S.and/orothercountries.
Helvetica is a registered trademark of
Heidelberger Druckmaschinen AG, available
from Linotype Library GmbH.
OpenGL is a registered trademark of Silicon
Graphics, Inc.
EventhoughApplehasreviewedthisdocument,
APPLEMAKESNOWARRANTYORREPRESENTATION,
EITHEREXPRESSORIMPLIED,WITHRESPECTTO
THISDOCUMENT,ITSQUALITY,ACCURACY,
MERCHANTABILITY,ORFITNESSFORAPARTICULAR
PURPOSE.ASARESULT,THISDOCUMENTIS
PROVIDED“ASIS,”ANDYOU,THEREADER,ARE
ASSUMINGTHEENTIRERISKASTOITSQUALITY
ANDACCURACY.
INNOEVENTWILLAPPLEBELIABLEFORDIRECT,
INDIRECT,SPECIAL,INCIDENTAL,OR
CONSEQUENTIALDAMAGESRESULTINGFROMANY
695673697.003.png
Contents
Introduction IntroductiontoCocoaDrawingGuide 13
Who Should Read This Document 13
Organization of This Document 13
See Also 14
Chapter1 OverviewofCocoaDrawing 15
Cocoa Drawing Support 15
The Painter’s Model 16
The Drawing Environment 17
The Graphics Context 17
The Graphics State 17
The Coordinate System 18
Transforms 18
Color and Color Spaces 19
Basic Drawing Elements 19
Geometry Support 19
Shape Primitives 20
Images 21
Gradients 22
Text 22
Views and Drawing 22
Common Drawing Tasks 23
Chapter2 GraphicsContexts 25
Graphics Context Basics 25
The Current Context 26
Graphics State Information 27
Screen Canvases and Print Canvases 29
Graphics Contexts and Quartz 30
Modifying the Current Graphics State 30
Setting Colors and Patterns 30
Setting Path Attributes 31
Setting Text Attributes 31
Setting Compositing Options 31
Setting the Clipping Region 34
Setting the Anti-aliasing Options 36
Creating Graphics Contexts 37
Creating a Screen-Based Context 37
Creating a PDF or PostScript Context 37
2011-01-18 | © 2005, 2011 Apple Inc. All Rights Reserved.
3
695673697.004.png
CONTENTS
Threading and Graphics Contexts 38
Chapter3 CoordinateSystemsandTransforms 39
Coordinate Systems Basics 39
Local Coordinate Systems 39
Points Versus Pixels 40
Resolution-Independent User Interface 42
Transform Basics 42
The Identity Transform 43
Transformation Operations 43
Transformation Ordering 45
Transform Mathematics 46
Using Transforms in Your Code 47
Creating and Applying a Transform 47
Undoing a Transformation 48
Transforming Coordinates 49
Converting from Window to View Coordinates 49
Flipped Coordinate Systems 50
Configuring Your View to Use Flipped Coordinates 51
Drawing Content in a Flipped Coordinate System 51
Creating a Flip Transform 54
Cocoa Use of Flipped Coordinates 54
Doing Pixel-Exact Drawing 55
Tips for Resolution Independent Drawing in Cocoa 56
Accessing the Current Scale Factor 56
Adjusting the Layout of Your Content 57
Converting Coordinate Values 57
Chapter4 ColorandTransparency 59
About Color and Transparency 59
Color Models and Color Spaces 59
Color Objects 60
Color Component Values 60
Transparency 60
Pattern Colors 61
Color Lists 61
Color Matching 62
Creating Colors 62
Working with Colors 62
Applying Colors to Drawn Content 63
Applying Color to Text 63
Getting the Components of a Color 64
Choosing Colors 64
Working with Color Spaces 64
4
2011-01-18 | © 2005, 2011 Apple Inc. All Rights Reserved.
695673697.005.png
CONTENTS
Converting Between Color Spaces 65
Mapping Physical Colors to a Color Space 65
Chapter5 Paths 67
Path Building Blocks 67
The NSBezierPath Class 67
Path Elements 68
Subpaths 69
Path Attributes 70
Winding Rules 75
Manipulating Geometric Types 77
Drawing Fundamental Shapes 78
Adding Points 78
Adding Lines and Polygons 79
Adding Rectangles 79
Adding Rounded Rectangles 80
Adding Ovals and Circles 81
Adding Arcs 82
Adding Bezier Curves 84
Adding Text 84
Drawing the Shapes in a Path 85
Drawing Rectangles 86
Working with Paths 87
Building Paths 87
Improving Rendering Performance 87
Manipulating Individual Path Elements 89
Transforming a Path 89
Creating a CGPathRef From an NSBezierPath Object 90
Detecting Mouse Hits on a Path 91
Chapter6 Images 93
Image Basics 93
Image Representations 94
Images and Caching 96
Image Size and Resolution 98
Image Coordinate Systems 99
Drawing Versus Compositing 99
Supported Image File Formats 100
Basic Formats 100
TIFF Compression 101
Support for Other File Formats 102
Guidelines for Using Images 103
Creating NSImage Objects 103
Loading an Existing Image 104
2011-01-18 | © 2005, 2011 Apple Inc. All Rights Reserved.
5
695673697.001.png
Zgłoś jeśli naruszono regulamin