在加载过程中在Flash中显示渐进式JPEG

Peh*_*hat 8 flash jpeg actionscript-3 progressive

我需要以渐进式JPEG格式显示图像(http://en.wikipedia.org/wiki/JPEG#JPEG_compression,不要与顺序JPEG的逐行显示混淆).Flash支持加载渐进式JPEG,但我不知道如何在加载过程中显示它.简短的谷歌搜索让我逐步加载顺序JPEG,没有别的.

Cay*_*Cay 4

事情会是这样的:

// 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,loadBytes 过程是异步的。另外,当您使用不可解析的字节数组尝试它时(通常是第一次调用 onProgress,当没有足够的图像数据来处理时),它会默默地失败,所以您需要以某种方式保证您有足够的数据......在这种情况下我使用if(bytes.length > 4096) ;)