URLLoader data for BitmapData

I am trying to upload an image file that is next to a .SWF file. Something like that:

var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, function(e:Event):void {
    trace(typeof(loader.data));
    graphic = spritemap = new Spritemap(loader.data, 32, 32);
    ...
}

But this is the result that I get:

object
[Fault] exception, information=Error: Invalid source image.

The fact is that loader.data has image bytes, but is not an instance of BitmapData and what Spritemap expects.

How to convert to BitmapData?

thank

+5
source share
1 answer
// define image url
var url:URLRequest = new URLRequest("http://sstatic.net/ads/img/careers2-ad-header-so.png");

// create Loader and load url
var img:Loader = new Loader();
img.load(url);

// listener for image load complete
img.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);

// attaches the image when load is complete
function loaded(e:Event):void
{
    var bitmap:Bitmap = e.target.content;
    doStuffWithBitmapData(bitmap.bitmapData);

    addChild(bitmap);

    // remove listener
    e.target.removeEventListener(Event.COMPLETE, loaded);
}

/**
 * Handle loaded BitmapData
 * @param bmd The loaded BitmapData
 */
function doStuffWithBitmapData(bmd:BitmapData):void
{
    trace(bmd);

    // your code
}

Basically;

You should use Loader, not URLLoader. You can access the BitmapDatadownloaded Bitmapusing bitmap.bitmapData.

+18
source

All Articles