Search code examples
apache-fleximageactionscriptflex4

Why img doesn't load in actionscript?


I would like to display an Image using a Bitmap as source. I've been suggested something akin to this but somehow it still doesn't work.

img1 works fine... But img2 doesn't load for some reason.

private function onComplete(event:Event):void{
    _bytes = event.target.data;

    img1.source = _bytes;  /*this last bit works*/

    _bmpData = new BitmapData(img1.width,img1.height);

    _bmpData.draw(img1,new Matrix());

    _bmp = new Bitmap(_bmpData);

    img2.source=_bmp;
}

Solution

  • img2.source=_bmp; doesn't work because you can't pass a Bitmap object to the source property of an Image control. From the documentation:

    The value of the source property represents a relative or absolute URL; a ByteArray representing a SWF, GIF, JPEG, or PNG; an object that implements IFlexDisplayObject; a class whose type implements IFlexDisplayObject; or a String that represents a class.

    A Bitmap is a DisplayObject, but it does not implement IFlexDisplayObject, so instead of using Image.source you can add the Bitmap as a child of the Image:

    img2.addChild(_bmp);