Dave
Dave

Reputation: 108

Resizing dynamically loaded images Flash AS3

I have a series of banners (standard sizes) which all need to load the same corresponding image for each slide. I can load them fine but I want the image to match the size of the container MC that the image is being loaded to, is that possible? Either that or to set the height/width manually...

Everything I have tried doesnt work, here is the code for the working state (where it just loads the image which stretches across the stage)

Code:

var myImage:String = dynamicContent.Profile[0].propImage.Url;

function myImageLoader(file:String, x:Number, y:Number):StudioLoader{
    var myImageLoader:StudioLoader = new StudioLoader();
    var request:URLRequest = new URLRequest(file);
    myImageLoader.load(request);
    myImageLoader.x = -52;
    myImageLoader.y =-30;   
   return myImageLoader;
}


propImage1.addChild(loadImage(enabler.getUrl(myImage),-20,0));

Upvotes: 0

Views: 421

Answers (1)

akmozo
akmozo

Reputation: 9839

You can resize your loaded image after the Event.COMPLETE on the LoaderInfo (contentLoaderInfo) of your URLLoader is fired, so you can do like this :

var request:URLRequest = new URLRequest('http://www.example.com/image.jpg');
var loader:Loader = new Loader();   
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, on_loadComplete);
    function on_loadComplete(e:Event):void {
        var image:DisplayObject = loader.content;       
            image.x = 100;
            image.y = 100;
            image.width = 300;
            image.height = 200;
        addChild(image);        
    }
    loader.load(request);

Edit :

load_image('http://www.example.com/image.jpg');

function load_image(url){
    var request:URLRequest = new URLRequest(url);
    var loader:Loader = new Loader();   
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, on_loadComplete);
        function on_loadComplete(e:Event):void {
            add_image(loader.content);
        }
        loader.load(request);
}
function add_image(image:DisplayObject, _x:int = 0, _y:int = 0, _width:int = 100, _height:int = 100){
    image.x = _x;
    image.y = _y;
    image.width = _width;
    image.height = _height;
    addChild(image);    
}

Hope that can help.

Upvotes: 1

Related Questions