0

我想向我的应用程序显示几张图片

            sourceAudioFile =(event.target as File);
            sourceAudioFile.data;

            var myLoader:Loader = new Loader(); 
            var url :URLRequest = new URLRequest("file:///" + sourceAudioFile.nativePath);
            myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
            myLoader.load(url); 

注册监听器

private function onImageLoaded(e:Event):void 
{
            var image : Bitmap= new Bitmap(e.target.content.bitmapData);
            image.scaleX = 0.5 ; 
            image.scaleY = 0.5 ; 
}

有什么方法可以将一些自定义值添加到 Event.COMPLETE 以了解正在加载的图像类型。

像 e.imageName , e.noOfImage ,我可以在完整的处理程序中访问它们并在我注册此事件时为其提供值。

提前致谢

4

3 回答 3

1

是的,您可以尝试注册事件,例如:

myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,function(e:Event){

  dispatchEvent(new CustomEvent(CustomEvent.COMPLETE, "<imageType>"));

}, false, 0, true);

并添加新类,扩展事件类:

public class CustomEvent extends Event {

        public static const COMPLETE:String  = "CustomEventComplete";

        public var imageType:String = "";

        public function CustomEvent(type:String, imageType:String = "", bubbles:Boolean = false, cancelable:Boolean = false){

            super(type, bubbles, cancelable);

            this.imageType = imageType;
        }

        override public function clone():Event {

            return new CustomEvent(type, customTarget, bubbles, cancelable);
        }

    }

现在您可以注册 CustomEvent.COMPLETE 并在侦听器中获取图像类型。

于 2012-12-03T07:31:04.387 回答
1

您可以通过检查 LoaderInfo 的 url 属性来了解已加载的内容

private function onImageLoaded(e:Event):void 
{
var cli:LoaderInfo = e.target as LoaderInfo;//you added listener to the contentLoaderInfo property
    trace(cli.url);//url of the media being loaded
    //do something with it
}

同样在这个 SO 问题中,您可以更复杂地回答您的问题:)

将参数传递给事件监听器

于 2012-12-03T07:22:29.877 回答
1

是的,有一个简单的方法:

myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded(imageName, noOfImage));
myLoader.load(url);

function onImageLoaded(imageName:String, noOfImage:int):Function {
  return function(e:Event):void {
    // Respective "e", "imageName" and "noOfImage" are available for you here.
  }
}

如果您以后需要删除它,请将第一行更改为:

var functionOnImageLoaded:Function = onImageLoaded(imageName, noOfImage);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, functionOnImageLoaded);

然后稍后就这样做:

myLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, functionOnImageLoaded);

我以前回答过这个问题。看这里。

于 2012-12-04T21:31:32.757 回答