Actionscript 3 如何在加载ByteArray时获取图像尺寸?

Actionscript 3 如何在加载ByteArray时获取图像尺寸?,actionscript-3,air,Actionscript 3,Air,使用AS3和AIR,我希望在用ByteArray加载图像后获得图像尺寸。使用下面的代码,我可以成功地加载图像,甚至成功地应用scaleX/scaleY,但从加载大小中获得零高度/宽度 private function listFiles():void{ imagesPath=layout.pathFld.text; fileList=new Array; var desktop:File = File.userDirectory.r

使用AS3和AIR,我希望在用ByteArray加载图像后获得图像尺寸。使用下面的代码,我可以成功地加载图像,甚至成功地应用scaleX/scaleY,但从加载大小中获得零高度/宽度

        private function listFiles():void{
        imagesPath=layout.pathFld.text;
        fileList=new Array;
        var desktop:File = File.userDirectory.resolvePath(imagesPath);
        var files:Array = desktop.getDirectoryListing();
        for (var i:uint = 0; i < files.length; i++)
        {
            fileList.push(files[i].nativePath);
        }
        for (i = 0; i < fileList.length; i++)
        {
            layout.txtFld.appendText(fileList[i]+"\n");
        }
        loadOneImage();
    }

    private function loadOneImage():void
    {
        var f:String=fileList[counter];
        bytes = new ByteArray();
         myFileStream = new FileStream();
        var myFile:File = File.userDirectory.resolvePath(f);

        myFileStream.addEventListener(ProgressEvent.PROGRESS, progressHandler);
        myFileStream.addEventListener(Event.COMPLETE, loadImage);       
        myFileStream.openAsync(myFile, FileMode.READ);
    }

    private function progressHandler(event:ProgressEvent):void 
    {
        if (myFileStream.bytesAvailable)
        {
            myFileStream.readBytes(bytes, myFileStream.position, myFileStream.bytesAvailable);
        }
    }

    private function loadImage(e:Event):void
    {
        var loader:Loader = new Loader();
        loader.loadBytes(bytes);    
        loader.scaleX=.05;
        loader.scaleY=.05;
        var ph:Number=loader.height;
        var pw:Number=loader.width;
        trace("ph/pw="+ph+"/"+pw);
       // I GET ZEROS HERE
     }
私有函数listFiles():void{
imagesPath=layout.pathFld.text;
fileList=新数组;
var desktop:File=File.userDirectory.resolvePath(imagesPath);
var文件:Array=desktop.getDirectoryListing();
对于(变量i:uint=0;i
为了正确获取构建的
显示对象的大小,首先将其添加到stage。若你们不需要把它放在舞台上,你们可以在一个四行代码的空间里添加、获取大小、删除所有内容。此外,即使您执行
loadBytes()
,加载程序也会异步执行加载,因此在尝试获取大小之前,您仍然必须先侦听
事件。完成

要获取图像尺寸,您可以使用
loader.contentLoaderInfo
,但正如@Vesper所说,您必须等待触发它的
事件.COMPLETE
事件,否则编译器将触发错误,所以您可以这样做:

var loader:Loader = new Loader();
    loader.loadBytes(bytes);
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onContentLoad);
    function onContentLoad(e:Event):void{
        trace('image width : ' + e.target.width);
        trace('image height : ' + e.target.height);         
    }
希望这能有所帮助