1 Reply Latest reply on Sep 7, 2006 1:29 AM by jpwrunyan

    Image loader bug when setting source data dynamically

    jpwrunyan Level 1
      I scanned old posts but saw no reference to the following problem/bug with Image/SWFLoader class.

      The problem is that when I load different images into the same Image instance the Image instance does not dispose of the old bitmap data when the source changes. For example, I have an application with an Image view that shows .jpg's whose names and source are in a List. A user clicks an item in the list and the image is loaded into the Image instance (source property is set dynamically). What is completely unforgivable is that it does even have the good sense to use the same bitmap data if I reload the same image source.

      Is there no way to get the Image class to flush its bitmap content?

      The only solution I can think of is to try deleting and re-instantiating the Image instance when the user selects a new image to view.

      (BTW, I tested the above in Flex 1.5 and, of course, the old Loader class completely disposed of old content when loading new content--just as it should in Flex 2.0!)
        • 1. Re: Image loader bug when setting source data dynamically
          jpwrunyan Level 1
          I found a way to delete the bitmap data from the loader on GSkinner's blog:
          http://www.gskinner.com/blog/archives/2006/08/as3_resource_ma_2.html

          The trick is to access the bitmap data and call the dispose() method to clear it from memory. Why this wasn't implemented in the Image class to begin with is still a mystery to me....

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="vertical">

          <mx:Script>
          <![CDATA[
          import mx.controls.Image;
          private var blorch:Image;
          private function add():void {
          blorch = new Image();
          blorch.source = "images/P001.jpg";
          blorch.width = 100;
          blorch.height = 100;
          this.addChild(blorch);
          }
          private function remove():void {

          var bmp:Bitmap = blorch.content as Bitmap;
          var bmpData:BitmapData = bmp.bitmapData;
          bmpData.dispose();
          this.removeChild(blorch);

          }
          ]]>
          </mx:Script>

          <mx:Button label="add" click="add()" />
          <mx:Button label="remove dammit!" click="remove()" />

          </mx:Application>