FLEX 4 :选择本地文件编辑

代码如下:

<?xml version="1.0" encoding="utf-8"?>

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
      xmlns:s="library://ns.adobe.com/flex/spark"
      xmlns:mx="library://ns.adobe.com/flex/halo"
      minWidth="1024" minHeight="768" creationComplete="init();"
      xmlns:mx1="library://ns.adobe.com/flex/mx">
 
 <s:layout>
  
  <s:HorizontalLayout/>
  
 </s:layout>
 
 <fx:Script>

  <![CDATA[
   
   import mx.core.UIComponent;
   
   import flash.net.FileFilter;
   
   import flash.net.FileReference;
   
   
   
   private var imageFilter:FileFilter=
    new FileFilter("图片文件(*.jpeg;*.jpg;*.png;*.gif)","*.jpeg;*.jpg;*.png;*.gif");
   
   private var imageReference:FileReference=new FileReference();
   
   private var imageLoader:Loader=new Loader();
   
   
   
   private function init():void{
    
    var uicomp:UIComponent=new UIComponent();
    
    uicomp.addChild(imageLoader);
    
    canvas.addElement(uicomp);
    
   }
   
   private function loadImage():void{
    
    imageReference.browse([imageFilter]);
    
    imageReference.addEventListener(Event.SELECT,fileSelected);
    
   }
   
   private function fileSelected(event:Event):void{
    
    imageReference.addEventListener(Event.COMPLETE,fileComplete);
    
    imageReference.addEventListener(ProgressEvent.PROGRESS,fileProgress);
    
    imageReference.load();
    
   }
   
   private function fileComplete(event:Event):void{
    
    imageLoader.loadBytes(imageReference.data);
    
   }
   
   private function fileProgress(event:ProgressEvent):void{
    
    
    
   }
   
  ]]>
  
 </fx:Script>
 <s:Button label="LoadImage" click="loadImage();"/>
 <mx1:Canvas id="canvas" width="553" height="437" borderColor="0x000000" borderStyle="solid">
 </mx1:Canvas>
 
</s:Application>

遇到的后续问题:

    加载本地图片后舞台的大小变化。 即获得本地图片的width 和 height 等原始属性。

解决办法:

  通过 Loader 类 的 contentLoaderInfo 属性 : var target:LoaderInfo = evt.currentTarget as LoaderInfo;

  代码如下:

 private function fileComplete(event:Event):void
  {
   
     loder.loadBytes(file.data);
   
     loder.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHander);
   
  }

 private function completeHander(evt:Event):void
  {
     var target:LoaderInfo = evt.currentTarget as LoaderInfo;
   
     img.width = target.width;
   
     img.height = target.height;  // 即图片宽高
   
  }

转载于:https://www.cnblogs.com/liuzhong/archive/2011/07/27/2118559.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值