13.07.2015 Views

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

216CHAPTER 7: User Interfaces: The Application KitNote that the only drawing being done here is to place a gradient in the middle of a whiterectangle; instantly the illusion of two separate views overlapping one another is created.ImagesImages are handled using the NSImage class. This is a reasonably complex class, as it handlesnot only bitmaps but also compressed images in a variety of formats. It also needs to managebitmap output representations for a variety of targets: a screen at 72dpi (dots per inch) isthe most common, but Retina Display Macs use double this resolution, and printers can useanywhere from 50 to many hundreds of dots per inch. NSImage handles this all for you in astransparent a manner as possible.Ordinarily you include your own images within your application’s bundle. For example, to loadMyArtwork.png you simply call [NSImage imageNamed: @"MyArtwork"]. When loading imagesfrom elsewhere, you can use a similarly simple method: [[NSImage alloc]initByReferencingURL: aURL].If you want to actually create an image using the other graphics primitives discussed in thischapter, you can create one by using [NSImage imageWithSize:aSize flipped:shouldBeFlippeddrawingHandler: ^BOOL(NSRect rect){ ...}], which will create the image and call the providedblock to draw its content for you. This method was added in OS X 10.8; previously you wouldhave needed to create the image using [[NSImage alloc] initWithSize:aSize], then called[myImage lockFocus] (or -lockFocusFlipped), drawn into the active NSGraphicsContext, thencalled [myImage unlockFocus].Listing 7-9 shows a simple example that illustrates both methods of creating an arbitrary imagecontaining two rectangles.Listing 7-9. Drawing into an NSImage// On OS X 10.8:NSImage * myImage = [NSImage imageWithSize:NSMakeSize(100,100) flipped:NOdrawingHandler: ^BOOL(NSRect rect) {[[NSColor whiteColor] setFill];NSRectFill(rect);NSRect square = NSMakeRect(0.0,0.0,50.0,50.0);// draw a square in the lower-left quadrant[[NSColor lightGrayColor] setFill];NSRectFill(square);// draw a square in the upper-right quadrantsquare.origin.x = square.origin.y = 50.0;[[NSColor darkGrayColor] setFill];NSRectFill(square);}];www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!