Caching images in your Flex application can greatly improve performance and reduce the overhead of loading external resources.
And I’m not simply talking about using the cacheAsBitmap property to improve rendering performance or the cachePolicy property to speed up animations. I’m talking about caching the actual bitmap data of an image.
To get started, you’ll need a hash map to store the image data. A Dictionary or an associative array will also work just fine.
Loading an image for the first time is the same as usual. You create a new Image object and add a listener for the COMPLETE event:
|
var image : Image = new Image (); image.addEventListener (Event.COMPLETE, onImageComplete); |
Once the image has finished loading, you add a copy of the bitmap data to the hash map using the image URL as the hash key:
|
private function onImageComplete (event : Event) : void { var image : Image = event.target as Image; if (! imageCache.containsKey (image.source)) { var bitmapData : BitmapData = new BitmapData (image.content.width, image.content.height, true); bitmapData.draw (image.content); imageCache.put (image.source, bitmapData); } } |
Now we can use the image as many times as we want without ever having to load it again!
To take advantage of this, you’ll need to check the hash map every time you create a new image (or change the source property) to see if you’ve already cached it:
|
var image : Image = new Image (); image.addEventListener (Event.COMPLETE, onImageComplete); if (imageCache.containsKey (imageURL)) { image.source = new Bitmap (imageCache.getValue (imageURL)); } else { image.source = imageURL; } |
And that’s it!
If you have an application that loads a large number of images you may want to limit the number of cached images to prevent the Flash player memory usage from getting out of hand, but in general caching even several dozen large images only results in a slightly increased footprint.

8 comments
Comments feed for this article
July 31, 2008 at 2:03 am
Flexで動的画像キャッシュを試す | ClockMaker Blog
[...] Flexで画像キャッシュの方法が紹介されてましたので試してみました。2度目以降の読み込みはキャッシュから利用され、通信が発生していないことが確認できます。FirefoxのアドオンであるFirebugやLiveHTTPHeadersを使うことでFlash内から二度目の通信が発生していないことが確認できます。 [...]
September 4, 2008 at 12:55 pm
Scaling Bitmap Data « Miscellanea
[...] 4, 2008 in ActionScript, Flex | Tags: code, development | I’ve posted before on caching and smooth-scaling images, but these tips don’t apply when you want to manipulate bitmap data [...]
September 14, 2008 at 2:41 am
Available cache image by Flex | ClockMaker Demos
[...] How to Cache Images in Your Flex Application « Miscellanea [...]
January 22, 2009 at 9:28 am
carvalhar
hey, i’ve some doubts.
how do you tell the source?
image.source =”path”; ?
and about imageCache? how do you declare it? who is it?
January 29, 2009 at 3:31 pm
Nick Schneble
@carvalhar
When you load an image from the hash map, image.source will point to the actual bitmap data, and not the path to the image. If you’d like to keep a reference to the path, you can always create a custom image class that saves it as a separate property.
imageCache is a hash map you declare somewhere in your application. It’s where you store the bitmap data for the images you’ve loaded. Like I said above, it can be a dictionary, an associative array, or a hash map like the one I use in my examples.
March 19, 2009 at 2:27 pm
andreaarg
If I’m developing a MXML component which is used as an item renderer for a tilelist… where should Event.Complete be placed? I tried “addEventListener” and “complete” event in the tag, but it doesn’t trigger.
March 24, 2009 at 10:46 am
Nick Schneble
@andreaarg
Add a “creationComplete” event handler to your component. You should be able to do this at the top of the item declaration where the XML namespaces are defined.
August 18, 2009 at 8:04 pm
Creating an Image Cache with ActionScript 3 « The bAS3
[...] I started looking around for a solution, and came across this question on StackOverflow.com as well as an excellent pseudo-code example: this blog post. [...]