Flex使用Loader和URLRequest加载本地图片示例:
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
- layout="absolute">
- <mx:Script>
- <![CDATA[
- import mx.core.UIComponent;
- import mx.controls.Alert;
- import flash.display.*;
- import flash.net.URLRequest;
- var ldr:Loader;
- private function Load():void
- {
- ldr= new Loader();<span id="more-684"></span>
- var url:String="tuzi.jpg";
- var urlReq:URLRequest=new URLRequest(url);
- ldr.load(urlReq);
- ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
- }
- private function completeHandler(e:Event):void
- {
- //mx.controls.Alert("load");
- var uc:UIComponent=new UIComponent();
- uc.addChild(ldr);
- thePanel.addChild(uc);
- ldr.scaleX*=thePanel.width / ldr.width; //按照比例缩放
- ldr.scaleY*=thePanel.height / ldr.height;
- ldr.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler);
- }
- ]]>
- </mx:Script>
- <mx:Button x="10"
- y="21"
- label="Load"
- click="Load()"
- width="69"
- height="20"
- themeColor="#009DFF"
- fillAlphas="[1.0, 1.0, 1.0, 1.0]"
- fillColors="[#FFFFFF, #FFFFFF]"
- color="#0B333C"/>
- <mx:Panel x="10"
- y="63"
- width="448"
- headerHeight="0"
- borderThicknessLeft="0"
- borderThicknessRight="0"
- height="376"
- layout="absolute"
- id="thePanel">
- </mx:Panel>
- </mx:Application>
Flex中通过设置autoLoad属性阻止Image控件自动加载图片的例子:
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
- layout="vertical"
- verticalAlign="middle"
- backgroundColor="white">
- <mx:ApplicationControlBar dock="true">
- <mx:Button id="btn"
- label="Load Image"
- click="img.load();" />
- </mx:ApplicationControlBar>
- <mx:Image id="img"
- autoLoad="false"
- source="assets/Fx.png"
- maintainAspectRatio="true"
- percentWidth="100"
- percentHeight="100" />
- </mx:Application>
如果要加载远程的图片,需要考虑安全沙箱问题。可以考虑使用LCDS代理,或者使用PHP,JS代理先取回图片再用AS去连。