ImageCache Class Reference

A global cache of images that have been loaded from files or memory. More...

Inherits DeletedAtShutdown, and Timer.

List of all members.

Static Public Member Functions

static ImagegetFromFile (const File &file)
 Loads an image from a file, (or just returns the image if it's already cached).
static ImagegetFromMemory (const void *imageData, int dataSize)
 Loads an image from an in-memory image file, (or just returns the image if it's already cached).
static void release (Image *imageToRelease)
 Releases an image that was previously created by the ImageCache.
static void releaseOrDelete (Image *imageToRelease)
 Releases an image if it's in the cache, or deletes it if it isn't cached.
static bool isImageInCache (Image *imageToLookFor)
 Checks whether an image is in the cache or not.
static void incReferenceCount (Image *image)
 Increments the reference-count for a cached image.
static ImagegetFromHashCode (int64 hashCode)
 Checks the cache for an image with a particular hashcode.
static void addImageToCache (Image *image, int64 hashCode)
 Adds an image to the cache with a user-defined hash-code.
static void setCacheTimeout (int millisecs)
 Changes the amount of time before an unused image will be removed from the cache.

Detailed Description

A global cache of images that have been loaded from files or memory.

If you're loading an image and may need to use the image in more than one place, this is used to allow the same image to be shared rather than loading multiple copies into memory.

Another advantage is that after images are released, they will be kept in memory for a few seconds before it is actually deleted, so if you're repeatedly loading/deleting the same image, it'll reduce the chances of having to reload it each time.

See also:
Image, ImageFileFormat

Member Function Documentation

static Image* ImageCache::getFromFile ( const File file  )  [static]

Loads an image from a file, (or just returns the image if it's already cached).

If the cache already contains an image that was loaded from this file, that image will be returned. Otherwise, this method will try to load the file, add it to the cache, and return it.

It's very important not to delete the image that is returned - instead use the ImageCache::release() method.

Also, remember that the image returned is shared, so drawing into it might affect other things that are using it!

Parameters:
file the file to try to load
Returns:
the image, or null if it there was an error loading it
See also:
release, getFromMemory, getFromCache, ImageFileFormat::loadFrom
static Image* ImageCache::getFromMemory ( const void *  imageData,
int  dataSize 
) [static]

Loads an image from an in-memory image file, (or just returns the image if it's already cached).

If the cache already contains an image that was loaded from this block of memory, that image will be returned. Otherwise, this method will try to load the file, add it to the cache, and return it.

It's very important not to delete the image that is returned - instead use the ImageCache::release() method.

Also, remember that the image returned is shared, so drawing into it might affect other things that are using it!

Parameters:
imageData the block of memory containing the image data
dataSize the data size in bytes
Returns:
the image, or null if it there was an error loading it
See also:
release, getFromMemory, getFromCache, ImageFileFormat::loadFrom
static void ImageCache::release ( Image imageToRelease  )  [static]

Releases an image that was previously created by the ImageCache.

If an image has been returned by the getFromFile() or getFromMemory() methods, it mustn't be deleted directly, but should be released with this method instead.

See also:
getFromFile, getFromMemory
static void ImageCache::releaseOrDelete ( Image imageToRelease  )  [static]

Releases an image if it's in the cache, or deletes it if it isn't cached.

This is a handy function to use if you want to delete an image but are afraid that it might be cached.

static bool ImageCache::isImageInCache ( Image imageToLookFor  )  [static]

Checks whether an image is in the cache or not.

Returns:
true if the image is currently in the cache
static void ImageCache::incReferenceCount ( Image image  )  [static]

Increments the reference-count for a cached image.

If the image isn't in the cache, this method won't do anything.

static Image* ImageCache::getFromHashCode ( int64  hashCode  )  [static]

Checks the cache for an image with a particular hashcode.

If there's an image in the cache with this hashcode, it will be returned, otherwise it will return zero.

If an image is returned, it must be released with the release() method when no longer needed, to maintain the correct reference counts.

Parameters:
hashCode the hash code that would have been associated with the image by addImageToCache()
See also:
addImageToCache
static void ImageCache::addImageToCache ( Image image,
int64  hashCode 
) [static]

Adds an image to the cache with a user-defined hash-code.

After calling this, responsibilty for deleting the image will be taken by the ImageCache.

The image will be initially be given a reference count of 1, so call the release() method to delete it.

Parameters:
image the image to add
hashCode the hash-code to associate with it
See also:
getFromHashCode
static void ImageCache::setCacheTimeout ( int  millisecs  )  [static]

Changes the amount of time before an unused image will be removed from the cache.

By default this is about 5 seconds.


The documentation for this class was generated from the following file:
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Mon Apr 26 11:42:12 2010 for JUCE by  doxygen 1.6.3