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.
A progressive JPEG image is encoded differently than a standard or baseline JPEG image. It loads in successive waves until a clear picture is formed. This can improve a website's performance as the images seems to be loading faster.
Less waiting time for usersWith baseline JPEG, your users see a large white space on the screen or a loading circle spinning until the image has finished loading. With progressive JPEG, visitors can already see the entire image at first sight. Although it's blurry, they can still understand what's in the image.
A progressive image is interlaced meaning the image will start out as low quality, however will continue to improve in resolution with each additional "pass". Traditionally, images load from the top-down (for JPEG images these are called Baseline JPEG).
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) ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With