Could be worth looking at storing a ByteArray and adding some
compression instead of storing the original BitmapData.
Tink
On 18 Apr 2008, at 19:15, handitan wrote:
Thanks for sharing the code, Eric.
Have you ever checked how much memory got increased whenever you load
your cached-img?
With my code, I always have a memory increase when I use the cached-
img because I always need to pass new Bitmap instance to image.load()
otherwise it will not load the image.
--- In [email protected], "Eric Cancil" <[EMAIL PROTECTED]> wrote:
>
> I made something fairly similar at one point
> http://3r1c.net/cacheableImages/
>
> On Thu, Apr 17, 2008 at 7:55 PM, handitan <[EMAIL PROTECTED]> wrote:
>
> > Hi all,
> >
> > I am trying to find a simple solution on caching images into the
memory.
> >
> > I had written a code to do that and it seemed to be working but I
am
> > pretty sure there are things that I had missed.
> >
> > Any feedback is greatly appreciated.
> >
> >
======================================================================
==
> >
> > I am using associative array to store the Bitmap and using the
image URL
> > path as the key.
> >
> > Here's how the code works in general:
> >
> > - You select which image that you want to load by choosing one of
the
> > image's name in the comboBox.
> >
> > - The selected image's name will be appended to a URL where the
image is
> > located. Let's called this new appended string: "newImgURL"
> >
> > - Check to see if "newImgURL" is existed in the associative
array. If it
> > is existed, then load the cached-bitmap else pass "newImgURL" to
> > imageObj.source.
> >
> > - If it's passed to imageObj.source, the imageObj will throw
a "complete"
> > event. On the completeEventHandler, cached the imageObj's Bitmap
to the
> > associative array.
> >
> > *Here's the code:*
> >
> > <?xml version="1.0" encoding="utf-8"?>
> >
> > <mx:Application
> > xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal"
> > creationComplete="imageInit()">
> >
> > <mx:Script>
> >
> > <![CDATA[
> >
> > *import* flash.utils.getTimer;
> >
> > *import* mx.collections.ArrayCollection;
> >
> > *import* mx.controls.ComboBox;
> >
> > *import* mx.events.ListEvent;
> >
> > [
> > *Bindable*]
> >
> > *private* *var* myArray:Array = [*"Select"*,
> >
> > *"YourImageNameHere1.jpg"*,
> >
> > *"YourImageNameHere2.jpg"*,
> >
> > *"YourImageNameHere3.jpg"*];
> >
> > *private* *const* ResourceURL:String = *"http://PutYourURL/"*;
> >
> > [
> > *Bindable*]
> >
> > *private* *var* time2:int = 0;
> >
> > *private* *var* loadTime2:int = 0;
> >
> > *private* *var* myDictionary:Array = *new* Array;
> >
> > [
> > *Bindable*]
> >
> > *private* *var* status:String = *"Not Updated"*;
> >
> > [
> > *Bindable*]
> >
> > *private* *var* totalMemoryNum:uint = System.totalMemory;
> >
> > [
> > *Bindable*]
> >
> > *private* *var* byteDiff:uint = 0;
> >
> > [
> > *Bindable*]
> >
> > *private* *var* myLoaderContext : LoaderContext;
> >
> > *private* *function* imageInit() : *void*
> >
> > {
> >
> > myLoaderContext =
> > *new* LoaderContext();
> >
> > myLoaderContext.checkPolicyFile =
> > *true*;
> >
> > }
> >
> > *//====================================*
> >
> > *//Using dictionary*
> >
> > *//====================================*
> >
> > *private* *var* exist:Boolean = *true*;
> >
> > *private* *function* loadImg2(event:ListEvent):*void*
> >
> > {
> >
> > *var* myImgName:String = (event.target *as*
ComboBox).selectedItem *as*String;
> >
> > *var* imgPath:String = ResourceURL + myImgName;
> >
> > *var* myDisplayObj:DisplayObject = myDictionary[imgPath];
> >
> > status =
> > *"Not Updated"*;
> >
> > time2 = 0;
> >
> > *if* (myDisplayObj != *null*)
> >
> > {
> >
> > *//trace("Cached version");*
> >
> > *var* myImage:Bitmap = myDisplayObj *as* Bitmap;
> >
> > FlipSource2.load(
> > *new* Bitmap(myImage.bitmapData));
> >
> > }
> >
> > *else*
> >
> > {
> >
> > exist =
> > *false*;
> >
> > FlipSource2.source = imgPath;
> >
> > }
> >
> > byteDiff = System.totalMemory - totalMemoryNum;
> >
> > totalMemoryNum = System.totalMemory;
> >
> > loadTime2 = getTimer();
> >
> > }
> >
> > *private* *function* takeDiffTime2():*void*
> >
> > {
> >
> > *if* (!exist)
> >
> > {
> >
> > status =
> > *"Updated"*;
> >
> > *var* myCloneBitmapData:BitmapData = (FlipSource2.content
*as*Bitmap).bitmapData.clone();
> >
> > *var* image:Bitmap = *new* Bitmap(myCloneBitmapData);
> >
> > myDictionary[FlipSource2.source] = image;
> >
> > }
> >
> > time2 = getTimer() - loadTime2;
> >
> > }
> >
> > ]]>
> >
> > </mx:Script>
> >
> > <mx:VBox width="100%" height="100%" >
> >
> > <mx:ComboBox dataProvider="{myArray}" labelField="label"
> > change="loadImg2(event)"/>
> >
> > <mx:Label id="LoadTime2" text="Total Load Time: {time2} ms -
{status}"/>
> >
> > <mx:HBox width="100%" height="100%" horizontalAlign="center">
> >
> > <mx:Image id="FlipSource2" width="480" height="480"
horizontalAlign="
> > center" complete="takeDiffTime2();*trace*(*'Image Load2
Complete'*)"
> > cachePolicy="off"
> >
> > cacheAsBitmap="
> > false"
> >
> > loaderContext="
> > {myLoaderContext}"/>
> >
> > </mx:HBox>
> >
> > </mx:VBox>
> >
> > <mx:VBox width="100%">
> >
> > <mx:Label id="TotalMemory" text="Total Memory:
{totalMemoryNum/1024} KB"/>
> >
> > <mx:Label id="DiffMem" text="Byte Diff: {byteDiff/1024} KB"/>
> >
> > <mx:Label text="Version 1.0" />
> >
> > </mx:VBox>
> >
> > </mx:Application>
> >
> >
> >
> >
> >
> >
>