This is not code I’m proud of, nor have I thoroughly tested it. Â However, I know SOMEONE out there will need this, so here it is. Â Keep in mind, she leaks memory like a seive, but for a slideshow that shows to the user for 30 seconds, who cares. Â If you’re bored, you’re welcome to fix/mod/improve/re-write.
What?
Client was mentioning that a slideshow I created for them was hitting the server each time requesting an image. Â Rather than go through the lengthy, and sometimes subjective process of describing why the browser cache can fail to work with Flash Player, I instead just made the images cache in RAM on the client side once downloaded.
How?
I tried to integrate Ely’s SuperImage code (since all but 1 class is pure ActionScript and not Flex related), and in retrospect, I think I got it working. Â My 2nd approach was to just create a simple local cache, and that worked.
Basically, once the image is downloaded, I make a Bitmap copy and store in a Dictionary. Â When you load a new image, I check to see if I have the image cached already.
Caveats
- Only works with sources that are String url’s
- Only works when loading bitmaps (not SWF’s)
- ByteArray’s are not supported (via loadBytes vs. setting source)
- I never remove the un-used Loaders
- I never remove the Bitmaps from cache
Source
Here’s UILoader. Â Also, I’ve included the fixed UIComponent as well (?) in case you actually want it to… you know, work.
Ah man, I didn’t see your tweet fast enough, I had a class file that I created similar to this one that just uses a Dictionary to cache bitmaps. Maybe it will help down the road: http://thanksmister.com/?p=336