Search code examples
actionscript-3flashactionscriptbitmapdata

How to display an image from URLRequest twice (downloading it only once)?


I am just wondering, if it is possible to load an image via URLRequest once, and display that image twice? Or do i have to load the image 2 times?

Edit: Also want to ask if it is possible to duplicate the bitmaps when they are loaded in a loop(array).

For example:

var duplicate:Array = new Array(loadedArray.push(e.target.content)); 

When i am trying the code :

        loadedArray.push(e.target.content);

        var duplicate:Array = new Array(loadedArray.push(e.target.content));

It doesnt give me any errors, but from before it adds the bitmap with no problem, but now, it doesnt add anything.

And when i tried

var duplicate:Array = new Array(loadedArray as Bitmap);
addChild(duplicate[0]);

I have an error Error #2007: Parameter child must be non-null.


Solution

  • Duplicating Bitmap or BitmapData is easy. There are a lot of options, these are few of them:

    // in your case, but can be any Bitmap
    var original:Bitmap = Bitmap(loader.content);
    
    // we actually need the bitmapData; separate for easy reading
    var originalBitmapData:BitmapData = original.bitmapData;
    
    var duplicate:Bitmap = new Bitmap(originalBitmapData);
    
    // this one returns brand new copy of the BitmapData (sometimes needed)
    var duplicate:Bitmap = new Bitmap(originalBitmapData.clone());
    

    copyPixels() on BitmapData is also super fast and can be used for duplication, depending if there is already Bitmap(Data) instantiated.