<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
initialize="appInit()"
layout="absolute"
viewSourceURL="srcview/index.html"
backgroundGradientAlphas="[1.0, 1.0]"
backgroundGradientColors="[#FFFFFF, #FFFFFF]">
<mx:Script>
<![CDATA[
import flash.net.URLStream;
import flash.net.URLRequest;
import flash.utils.getTimer;
public var loader:Loader;
public var imageStream:URLStream;
public var imageData:ByteArray;
public function appInit():void
{
imageStream = new URLStream();
imageStream.addEventListener( ProgressEvent.PROGRESS , imageStreamProgress );
imageStream.addEventListener( Event.COMPLETE , imageStreamComplete );
loader = new Loader();
imageCanvas.rawChildren.addChild( loader );
}
public function imageStreamProgress( event:Event ):void
{
if( imageStream.bytesAvailable == 0 ) return
this.processImageData();
}
public function imageStreamComplete( event:Event ):void
{
if ( imageStream.connected ) imageStream.close();
imageCanvas.callLater( this.processImageData );
}
public function processImageData():void
{
if ( imageStream.connected ) imageStream.readBytes( imageData , imageData.length );
loader.unload();
loader.loadBytes( imageData );
}
public function loadImage( input:String ):void
{
if ( imageStream.connected ) imageStream.close();
imageStream.load( new URLRequest( input + '?' + getTimer() ) );
loader.unload();
imageData = new ByteArray();
}
]]>
</mx:Script>
<mx:Button click="loadImage('http://onflex.org/flexapps/applications/ProgressiveImageLoading/png.png')" y="10" label="Load PNG1" x="10"/>
<mx:Button click="loadImage('http://onflex.org/flexapps/applications/ProgressiveImageLoading/tpng.png')" y="10" label="Load PNG2" x="105"/>
<mx:Button click="loadImage('http://onflex.org/flexapps/applications/ProgressiveImageLoading/gif.gif')" y="10" label="Load GIF" x="200"/>
<mx:Button click="loadImage('http://onflex.org/flexapps/applications/ProgressiveImageLoading/jpg.jpg')" y="10" label="Load JPG" x="285"/>
<mx:Canvas id="imageCanvas" left="10" top="40" right="10" bottom="10"/>
</mx:Application>