AS3 Image Loader - 它自己加载(swf)

时间:2011-12-11 06:04:08

标签: actionscript-3 file filereference

我的LoaderInfo将返回swf本身,而不是通过FileReference和Loader给出的图像。调试它时遇到问题,因为LoaderInfo.content返回[Object Main](我的文档类)。经过调查,我发现LoaderInfo.content是一个swf文件,根据contentType。

问题是,图像的文件引用是正确的(它是图像,而不是swf)。

我的代码:

    private function onAction(e:MouseEvent){
        if(e.currentTarget.name == 0){
            myFileReference = new FileReference();
            myFileReference.browse(getTypes());
            myFileReference.addEventListener(Event.SELECT, loadedImage);
            myFileReference.addEventListener(Event.COMPLETE, loadImage15);
        }
    }
    private function loadedImage(e:Event){
        var imgHolder:ImageHolder = Main.imageHolder;
        while(imgHolder.numChildren > 0){
            imgHolder.removeChild(imgHolder.getChildAt(0));
        }
        myFileReference.load();

    }
    private function loadImage15(e:Event){
        var loader:Loader = new Loader();
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadImg2);
        loader.loadBytes(myFileReference.data);
        trace(myFileReference.type); // .JPG
    }
    private function loadImg2(e:Event){
        var lInfo:LoaderInfo = (e.target as LoaderInfo); //e.target is indeed LoaderInfo
        lInfo.removeEventListener(Event.COMPLETE, loadImg2);
        trace(loaderInfo.contentType); //application/x-shockwave-flash
        var newSprite:MovieClip = loaderInfo.content as MovieClip;
        Main.imageHolde.addChild(newSprite); //Error as you can't addChild Main to Main
    }
    private function getTypes():Array {
        return [new FileFilter("Images","*.jpg;*.jpeg;*.gif;*.png")];
    }

2 个答案:

答案 0 :(得分:2)

修改

我原本有一个非常复杂的答案 - 这是错误的......

您的程序中只是出错:

// here you reference the Loader's contentLoaderInfo

var lInfo:LoaderInfo = (e.target as LoaderInfo); 
lInfo.removeEventListener(Event.COMPLETE, loadImg2);

// but from here on out, you reference your parent class' "loaderInfo" property!

trace(loaderInfo.contentType);
var newSprite:MovieClip = loaderInfo.content as MovieClip; // <- this is your Main class!
Main.imageHolde.addChild(newSprite); //Error as you can't addChild Main to Main

loaderInfo更改为lInfo,您应该没问题。

答案 1 :(得分:0)

我之前问过这个问题: loading image by Loader.loadBytes(byteArray)

没人知道:]

如果您使用Loader加载图像,您将会看到:

Loader.content is MovieClip
Loader.content.getChildAt(0) is Bitmap