Copy BitmapData From mx:Image

橙三吉。 提交于 2019-12-11 03:05:56

问题


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.


回答1:


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..!
}



回答2:


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



来源:https://stackoverflow.com/questions/3635144/copy-bitmapdata-from-mximage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!