Working with Images
NSImage
and
NSImageRep
are
Cocoa’s
image-handling workhorses. NSImage provides a
convenient and easy-to-use frontend to a powerful and flexible
backend comprised of NSImageRep’s
many subclasses. NSImage provides a high level
interface for loading, saving, and drawing images onscreen.
NSImageRep and its subclasses, on the other hand,
provide interfaces for working with specific image formats.
NSImage
NSImage
provides the high-level interface for
working with images, which includes the ability to load and save
images and draw them to screen. With NSImage, you
don’t need to worry about image formats, which are
handled internally by NSImage and the
NSImageRep set of classes. To initialize instances
of NSImage use these methods:
initByReferencingFile:initByReferencingURL:initWithContentsOfFile:initWithContentsOfURL:initWithData:initWithPasteboard:initWithSize:
When you initialize anything by reference, as with the first two
methods, the data is not loaded until the object actually needs it.
This contrasts with initWithContents..., which
loads the data and initializes the object immediately. The last
method, initWithSize:, initializes an empty image.
imageNamed: is a useful method for creating image
objects with images contained in the application bundle. With this
method, you don’t have to provide a path to the file
or include the file extension in the
name—NSImage knows where to find it within
the bundle.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access