Loading dynamically generated images in Binary
Recently I had to load in images from database references that were pointing to a .net serverside script. This path ended with something like ImageHandler.ashx?id=164
When I tried to load this in with my standard ImageLoader it didn’t work because it was coming back as binary and I was getting a reference error about the URL type not being found. I think it was
Error #2044: Unhandled IOErrorEvent:. text=Error #2124: Loaded file is an unknown type.
The way to get around this is a little strange and involves 2 loading processes. The first one loads in the data but set as
_loader.dataFormat = URLLoaderDataFormat.BINARY;
Then once this has loaded we ‘load’ the ByteArray that is returned into a loader and proceed as normal:
public function loadBinaryJPG(_path:String):void
{
_loader = new URLLoader();
_loader.addEventListener(Event.COMPLETE, _onLoadInit);
// set the dataFormat to binary
_loader.dataFormat = URLLoaderDataFormat.BINARY;
_loader.load(new URLRequest(_path));
}
private function _onLoadInit(e:Event):void
{
_loader.removeEventListener(Event.COMPLETE, _onLoadInit);
// once the binary data has loaded, check that there
// is data there and load that into a Loader
var _l2:Loader = new Loader();
_l2.contentLoaderInfo.addEventListener(Event.COMPLETE, _blComp);
var _byteArr:ByteArray = new ByteArray();
_byteArr = _loader.data;
// check that data was returned
if(_byteArr.length != 0)
{
_l2.loadBytes(_loader.data);
} else {
// generate error message
}
}
private function _blComp(e:Event):void
{
// deal with binary data that is in the loader (e.target)
_bmdata = new BitmapData(114, 87, false, 0xFFFFFF);
_bmdata = (e.target.content).bitmapData;
_bm = new Bitmap(_bmdata, "auto", true);
// deal with loaded Bitmap
}
Recent Comments