NSImage的缓存

来源:互联网 发布:淘宝买发票 编辑:程序博客网 时间:2024/06/05 19:10

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.

Table 6-2  Image caching modes

Mode

Description

NSImageCacheDefault

Use the default caching mode appropriate for the image representation. This is the default value. For more information, seeTable 6-3.

NSImageCacheAlways

Always caches a version of the image.

NSImageCacheBySize

Creates a cached version of the image if the size set for the image is smaller than the size of the actual image data.

NSImageCacheNever

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 toNSImageCacheDefault.

Table 6-3  Implied cache settings

Image representation

Cache behavior

NSBitmapImageRep

Behaves as if the NSImageCacheBySize setting were in effect. Creates a cached copy if the bitmap depth does not match the screen depth or if the bitmap resolution is greater than 72 dpi.

NSCachedImageRep

Not applicable. This class is used to implement caching.

NSCIImageRep

Behaves as if the NSImageCacheBySize setting were in effect. Creates a cached copy if the bitmap depth does not match the screen depth or if the bitmap resolution is greater than 72 dpi.

NSPDFImageRep

Behaves as if the NSImageCacheAlways setting were in effect.

NSEPSImageRep

Behaves as if the NSImageCacheAlways setting were in effect.

NSCustomImageRep

Behaves as if the NSImageCacheAlways setting were in effect.

NSPICTImageRep

Behaves as if the NSImageCacheBySize setting were in effect. Creates a cached copy of the PICT image if it contains a bitmap whose depth does not match the screen depth or if that bitmap resolution is greater than 72 dpi.

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, aNSPDFImageRep 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 therecache 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 theimage 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 theimage 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 asetDataRetained: 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.