Guidelines for Using Images
Here are some guidelines to help you work with images more effectively:
-
Use the
NSImage
interface whenever possible. The goal ofNSImage
is to simplify your interactions with image data. Working directly with image representations should be done only as needed. -
Treat
NSImage
and its image representations as immutable objects. The goal ofNSImage
is to provide an efficient way to display images on the target canvas. Avoid manipulating the data of an image representation directly, especially if there are alternatives to manipulating the data, such as compositing the image and some other content into a new image object. -
For screen-based drawing, it is best to use the built-in caching mechanism of
NSImage
. Using anNSCachedImageRep
object is more efficient than anNSBitmapImageRep
object with the same data. Cached image representations store image data using aCGImageRef
object, which can be stored directly on the video card by Quartz. -
There is little benefit to storing multiple representations of the same image (possibly at different sizes) in a single
NSImage
. Modern hardware is powerful enough to resize and scale images quickly. The only reason to consider storing multiple representations is if each of those representations contains a customized version of the image. -
If caching is enabled and you modify an image representation object directly, be sure to invoke the
recache
method of the owningNSImage
object. Cocoa relies on cached content wherever possible to improve performance and does not automatically recreate its caches when you modify image representations. You must tell the image object to recreate its caches explicitly. -
Avoid recreating art that is already provided by the system. Mac OS X makes several standard pieces of artwork available for inclusion in your own interfaces. This artwork ranges from standard icons to other elements you can integrate into your controls. You load standard images using the
imageNamed:
method. For a list of standard artwork, see the constants section in NSImage Class Reference.
Mac OS X defines several technologies for working with images. Although the NSImage
class is a good general purpose class for creating, manipulating, and drawing images, there may be times when it might be easier or more efficient to use other imaging technologies. For example, rather than manually dissolving from one image to another by drawing partially transparent versions of each image over time, it would be more efficient to use Core Image to perform the dissolve operation for you. For information about other image technologies, and when you might use them, see “Choosing the Right Imaging Technology.”