2 Replies Latest reply on Nov 25, 2010 10:16 PM by flex_developer_pk

    create cache variable for image


      hi everyone


      i am using swfloader to load an image, and storing its conents in an array so if it is loaded again in future it can be loaded directly from that variable instead of downloading it again.


      First time when i load the image , it is loaded properly, and when i load the same image secondtime, it loads it properly from this variable but old image is removed from the page. I am not able to understand what is causing this, here is the code which I used for this.



      public function loadimage(_data:Object){

      // check if it does not exists in cachedata variable then download  else load from cache

                      if(cachedata[_data.url] == null){
                          var _swfloader:SWFLoader=new SWFLoader;
                          _swfloader.addEventListener(Event.COMPLETE,EventDelegate.create(onImagesComplete,imgconta iner));
                      } else{



                  public function onImagesComplete(event:Event,imgcontainer):void
                      var __tpLd:SWFLoader = event.target as SWFLoader;
                      var imgContents=__tpLd.content;

      // store in cachedata variable


                      var extension:String=filename.substring(filename.length-3,filename.length);



      please help me on this...



        • 1. Re: create cache variable for image

          That is because you are caching _tpLd.content which will be a Bitmap object. So, as soon as you do an addChild again on it, it will be removed from the previous container as it can only have one parent.


          The solution is to cache BitmapData instead:


                    var bitmapData:BitmapData = (content as Bitmap).bitmapData;

                               _imageDataCache.putImageData(_imageId, bitmapData);
          So, you can re-use it by constructing a new Bitmap object from the same BitmapData. The difference in memory usage will be negligible as you are using the same BitmapData again.
          Here is an implementation of CachedImage component along with ImageDataCache so that we do not have to write plumbing code everywhere. You can use it or modify your code on the same lines. It is available at:
          • 2. Re: create cache variable for image
            flex_developer_pk Level 1

            you really saved my lot of time, thanks a lot