views:

60

answers:

2

How can I copy or duplicate the bitmapdata from a mx:image component?

I need to display the same image in multiple screens of my application and don't want to have to download the image multiple times.

I could just use a urlrequest to download the image as a bitmap and copy that but I like the way you can can just set the source of the image component.

A: 

Image extends SWFLoader which has a content property that will contain the Bitmap object that was loaded. Wait for the image to load, cast the content to Bitmap and read its bitmapData

public function imageLoadCompleteHandler(e:Event):void
{
    var bitmap:Bitmap = img.content as Bitmap;
    if(bitmap == null) {
        trace("loaded content is not an image");
        return;
    }
    bmpData = bitmap.bitmapData;
    //hurray..!
}
Amarghosh
cool.except for copy and pasting purposes the second line should read< var bitmap:Bitmap= e.currentTarget.content>I got errors on that line trying to cast a bitmap as an image
dubbeat
@dubbeat oops, it should be `Bitmap`, not `Image`. Fixed.
Amarghosh
A: 

Actually after reading the message above (and trying stuff out) I think this is wrong. The Complete listener fires on my second image so I guess it is loading it twice. Oh well.

I couldn't place the whole Flex doc, but this seemed to work. I added this to the Application tag: applicationComplete="Run();"

The following is wrapped in a mx:Script tag:

    import mx.controls.Image;
    private function Run():void
    {
        var i:Image = new Image();
        i.source = first.source;
        addChild(i);

    }

And this is just a Image outside of the script tag:

<mx:Image x="12" y="125" source="e53c04a51d5992fb77ab1e20c45ddc9f.jpeg" id="first" />

I also tried this after setting the i source:

first.source = null; 

Just to see what happens, the i image keeps it's source

Billy