apache-flexactionscript-3imageaironload-event

Adobe AIR - How can I catch the event of image loading completed?


Core of my code is following:

var img:Image = new Image;  
img.source = 'http://..........';  
img.autoLoad = true;  
img.cachePolicy = 'on';

img.addEventListener(Event.COMPLETE, function(event:Event):void {  
    trace('Loaded!', img.source);  
});  
img.addEventListener(SecurityErrorEvent.SECURITY_ERROR, function(event:Event):void {  
    trace('Error!', img.source);  
});  
img.addEventListener(IOErrorEvent.IO_ERROR, function(event:Event):void {  
    trace('Error!', img.source);  
});  

I found that, the complete event does not occur for some images. How can I catch complete event without signal leaks?


Solution

  • When you want to load an image (or even another swf) the class to use is Loader. A quick example:

    var loader:Loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, handlerFunction);
    loader.load(new URLRequest("http://somewhere/image.png"));
    

    The only kind of tricky thing is that the events related to the loading are dispatched by the loader.contentLoaderInfo object, not the loader object.

    And the always handy documentation: Loader Class