I was wondering if it is possible to implement any logic to wiat for the image to complete load without freezing.
For the sake of my app, I created a class which takes image url in constructor and loads the mx.controls.Image using the "load()" function.
See the class below
package com
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.*;
//import flash.net.URLRequest;
import mx.controls.Alert;
import mx.controls.Image;
//import mx.utils.OnDemandEventDispatcher;
public class SpriteImage extends Sprite
{
//Pass the source path or url here.
private var imageUrl:String;
private var ready:int = -1;
private var img:Image;
public function SpriteImage(imgUrl:String)
{
//imageUrl = imgUrl;
loadImage(imgUrl);
}
private function loadImage(imgUrl:String):void
{
img = new Image();
img.addEventListener(Event.COMPLETE,onCompleteHandler);
img.addEventListener(IOErrorEvent.IO_ERROR,onErrorHandler);
//img.source = imgUrl;
img.load(imgUrl);
}
private function onCompleteHandler(e:Event):void{
img.x = -(img.width/2);
img.y = -(img.height/2);
this.addChild(img);
ready = 1;
}
private function onErrorHandler(e:IOErrorEvent):void
{
ready = 0;
//Alert.show("Can't load :" + e.toString());
}
public function prepare():Boolean{
while(ready == -1){
trace(0);
}
return ready;
}
}
}
Now, I use this class from main app like this
var img:SpriteImage = new SpriteImage(e.imageUrl);
img.prepare(); // just waits till image is loaded but freezes
var obj:DisplayObject = img;
Now the problem is that the app never comes out of the prepare() method. It freezes the browser and I have to kill the plugin process to unfreez the app.
The main criteria here is that I need to wait for the image to complete loading so that I can access the image in the next steps. How can I do this.? Please help