loading image by Loader.loadBytes(byteArray)

2019-03-06 08:27发布

问题:

i like to ask about one thing :

If i create Loader and load external image by URLRequest , ill have result :

loader.content is Bitmap
loader.content.bitmapData is BitmapData

But if I use Loader.loadBytes(ImageBytes) , result is different even if ImageBytes is loader.contentLoaderInfo.bytes :

bytesLoader.content is MovieClip
bytesLoader.content.getChildAt(0) is Bitmap
bytesLoader.content.getChildAt(0).bitmapData is BitmapData

why ?

回答1:

AS3 Loader has internal parsing to try and match datatypes to internal class types. It's pretty handy in most cases, but the syntax is a little weird.

In your example above, you CAN cast the bytesLoader.content as Bitmap if you'd rather.

Edit (in reference to the "how" question):

ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, getImage);

ldr.load ( new URLRequest ( IMAGE_URL ) );

function getImage (e:Event):void {
    var bmp:Bitmap = ldr.content as Bitmap;
    addChild (bmp);
}

You should be able to simply cast it as a Bitmap.