Pehat
Pehat

Reputation: 1563

Display progressive JPEG in Flash during the loading

I need to display the image in progressive JPEG format ( http://en.wikipedia.org/wiki/JPEG#JPEG_compression , not to be confused with progressive display of sequential JPEG). Flash supports loading of progressive JPEG, but I have no idea how to display it during the loading. Brief googling gives me progressive loading of sequential JPEG and nothing else.

Upvotes: 8

Views: 883

Answers (1)

Cay
Cay

Reputation: 3794

It would go something like this:

// the loader containing the image
var loading:Boolean = false;
var loader:Loader = new Loader();
addChild(loader);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function() {
    loading = false;
    trace(loader.width, loader.height);
});

var bytes:ByteArray = new ByteArray();

var stream:URLStream = new URLStream();
stream.addEventListener(ProgressEvent.PROGRESS, onProgress);
stream.addEventListener(Event.COMPLETE, onProgress);
stream.load(new URLRequest(imageURL));

function onProgress(e:Event):void {
    stream.readBytes(bytes, bytes.length);
    if((bytes.length > 4096 && !loading) || e.type == Event.COMPLETE) {
        loading = true;
        loader.loadBytes(bytes);
    }
}

Notice that the loadBytes process is asynchronous. Also, when you try it with an unparseable bytearray (usually the first calls to onProgress, when there's not enough image data to process) it fails silently, so you need to somehow guarantee that you have enough data... in this case I used if(bytes.length > 4096) ;)

Upvotes: 4

Related Questions