The NSImage
class incorporates an internal caching scheme aimed at improving your application’s drawing performance. This caching scheme is an important part of image management and is enabled by default for all image objects; however, you can change the caching options for a particular image using the setCacheMode:
method of NSImage
. Table 6-2 lists the available caching modes.
Mode | Description |
---|---|
Use the default caching mode appropriate for the image representation. This is the default value. For more information, see Table 6-3. | |
Always caches a version of the image. | |
Creates a cached version of the image if the size set for the image is smaller than the size of the actual image data. | |
Does not cache the image. The image data is rasterized every time it is drawn. |
Table 6-3 lists the behavior of each image representation when its cache mode is set to NSImageCacheDefault
.
Image representation | Cache behavior |
---|---|
Behaves as if the | |
Not applicable. This class is used to implement caching. | |
Behaves as if the | |
Behaves as if the | |
Behaves as if the | |
Behaves as if the | |
Behaves as if the |
Caching is a useful step toward preparing an image for display on the screen. When first loaded, the data for an image representation may not be in a format that can be rendered directly to the screen. For example, PDF data, when loaded into a PDF image representation, must be rasterized before it can be sent to the graphics card for display. With caching enabled, a NSPDFImageRep
object rasterizes the PDF data before drawing it to the screen. The image representation then saves the raster data to alleviate the need to recreate it later. If you disable caching for such images, the rasterization process occurs each time you render the image, which can lead to a considerable performance penalty.
For bitmap image representations, the decision to cache is dependent on the bitmap image data. If the bitmap’s color space, resolution, and bit depth match the corresponding attributes in the destination device, the bitmap may be used directly without any caching. If any of these attributes varies, however, the bitmap image representation may create a cached version instead.
Important: It is important to remember that caching is aimed at improving performance during screen updates. During printing, Cocoa uses the native image data and resolution whenever possible and uses cached versions of the image only as a last resort.
If you change the contents of an image representation object directly, you should invoke the recache
method of the owning NSImage
object when you are done and want the changes to be reflected on the screen. Cocoa does not automatically track the changes you make to your image representation objects. Instead, it continues to use the cached version of your image representation until you explicitly clear that cache using the recache
method.
Caching and Image Data Retention
Because caching can lead to multiple copies of the image data in memory, NSImage
usually dismisses the original image data once a cached copy is created. Dismissing the original image data saves memory and improves performance and is appropriate in situations where you do not plan on changing the image size or attributes. If you do plan on changing the image size or attributes, you may want to disable this behavior. Enabling data retention prevents image degradation by basing changes on the original image data, as opposed to the currently cached copy.
To retain image data for a specific image, you must send a setDataRetained:
message to the NSImage
object. Preferably, you should send this message immediately after creating the image object. If you send the message after rendering the image or locking focus on it, Cocoa may need to read the image data more than once.