URLStream,URLLoader和Loader

URLSteam读取文本文件可以支持多种编码

URLLoader如果遇到乱码,则可以转换(就像上篇说的利用标准的xml格式Excle可以解决乱码问题就是基于这里):

  1.         var ba:ByteArray = new ByteArray;  
  2.         ba.writeBytes((event.target as URLLoader).data);  
  3.         ba.position = 0;  
  4.         //假如记载的是xml文档
  5.         var xml:XML = XML(ba.readMultiByte(ba.length, "GBK"));

loader用于加载外部的图片文件,SWF文件等。我之前在一个air里面用到它来上载图片,并且要生成一个缩略图,实现代码如下:

  1.             //import的相关
  2.             import mx.events.CloseEvent;
  3.             import flash.geom.Matrix;
  4.             import mx.graphics.codec.JPEGEncoder;
  5.             import mx.graphics.codec.PNGEncoder;
  6.             import mx.controls.Image;
  7.             import mx.controls.Alert;
  8.             import mx.utils.ArrayUtil;
  9.             import mx.collections.ArrayCollection;
  10.             import mx.formatters.NumberFormatter;
  11.             import mx.messaging.AbstractConsumer;
  12.             
  13.             
  14.             //压缩小图的相关信息信息
  15.             private var SmallPicSize:int = 80;
  16.             //保存相片的扩展名列表
  17.             private static const EXT_JPEG:String = "jpg";
  18.             private static const EXT_PNG:String = "png";        
  19.         
  20.         
  21.         
  22.         
  23.             public function browseAndUpload():void
  24.             {
  25.                 var FileToOpen:File = File.documentsDirectory;
  26.                 var txtFilter:FileFilter = new FileFilter("jpg,png","*.jpg;*png;"); 
  27.                 FileToOpen.browseForOpen("Open", [txtFilter]); 
  28.                 FileToOpen.addEventListener(Event.SELECT,FileToOpenSelected);   
  29.             }
  30.             //当上载的相片被打开后,就进行上载操作
  31.             private function FileToOpenSelected(objEvent:Event):void 
  32.             {
  33.                 var OrgImgPath:String = File(objEvent.currentTarget).nativePath;
  34.                 try
  35.                 {
  36.                     var orgFile:File = new File(OrgImgPath);
  37.                     //获取一个随机的图片名称
  38.                     var curPicName:String = "TestUploadPic."+orgFile.extension;
  39.                     //假如我们把图片的大图片放到项目的/upload/big/文件夹下
  40.                     var bigImgFile:File = new File(File.applicationDirectory.nativePath+"/upload/big/"+curPicName);
  41.                     orgFile.copyTo(bigImgFile); 
  42.                     //获取原图的bitmap信息,然后重构一个小图放到temp/small/文件夹下面
  43.                     var loader:Loader = new Loader();
  44.                     //使用内联函数在事件中传递参数
  45.                     var mySpecialObject:Object = {NewImgName:curPicName,NewImgExt:orgFile.extension};
  46.                     //注意是loader.contentLoaderInfo
  47.                     loader.contentLoaderInfo.addEventListener(Event.COMPLETE,function (e:Event) : void {loadComplete(e,mySpecialObject);});
  48.                     loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,loadError);
  49.                     //加载原图
  50.                     var request:URLRequest = new URLRequest(orgFile.nativePath);
  51.                     loader.load(request);
  52.                 }
  53.                 catch(er:Error)
  54.                 {
  55.                     Alert.show(er.message);
  56.                 }
  57.             }
  58.             
  59.              //在上载的过程中把原图也load进了内存,主要目的是用原图来生成一个小图,load这个动作完成后的处理方法 
  60.             public function loadComplete(event:Event,objSpecial:Object):void
  61.             {
  62.                 try
  63.                 {
  64.                     if(objSpecial != null && objSpecial.NewImgName.toString() != "")
  65.                     {
  66.                         var loader:Loader = Loader(event.target.loader);
  67.                         var BigBitmapdata:BitmapData = Bitmap(loader.contentLoaderInfo.content).bitmapData;
  68.                         var myscale:Number = 1.0;
  69.                         if(BigBitmapdata.width > 120 || BigBitmapdata.height > 100)
  70.                         {
  71.                             if(BigBitmapdata.width > BigBitmapdata.height)
  72.                             {
  73.                                 myscale = 120/BigBitmapdata.width;
  74.                             }else
  75.                             {
  76.                                 myscale = 100/BigBitmapdata.height;
  77.                             }
  78.                         }
  79.                         //创建新转换矩阵
  80.                         var m:Matrix = new Matrix();
  81.                         m.scale(myscale,myscale);
  82.                         var SmallBitmapdata:BitmapData = new BitmapData(BigBitmapdata.width*myscale,BigBitmapdata.height*myscale,BigBitmapdata.transparent,0xFFFFFFFF); 
  83.                         SmallBitmapdata.draw(BigBitmapdata,m);
  84.                         //相片的Bytearray
  85.                         var imgByteArray:ByteArray;
  86.                         switch(objSpecial.NewImgExt.toString())
  87.                         {
  88.                             case EXT_JPEG:
  89.                                 var jpgenc:JPEGEncoder = new JPEGEncoder(80);
  90.                                 imgByteArray = jpgenc.encode(SmallBitmapdata);
  91.                                 break;
  92.                             case EXT_PNG:
  93.                                 var pngenc:PNGEncoder = new PNGEncoder();
  94.                                 imgByteArray = pngenc.encode(SmallBitmapdata);
  95.                                 break;
  96.                         }
  97.                         //假如我们把图片的大图片放到项目的/upload/small/文件夹下
  98.                         var smallFile:File = new File(File.applicationDirectory.nativePath+"/upload/small/"+objSpecial.NewImgName.toString());
  99.                         var fs:FileStream = new FileStream();
  100.                         try{
  101.                             
  102.                             fs.open(smallFile,FileMode.WRITE);
  103.                             fs.writeBytes(imgByteArray);
  104.                             fs.close();
  105.                         }catch(e:Error){
  106.                             Alert.show(e.message);
  107.                         }   
  108.                         
  109.                         //释放BitmapData所占用的内存(很重要)
  110.                         BigBitmapdata.dispose();
  111.                         SmallBitmapdata.dispose();
  112.                     }   
  113.                 }
  114.                 catch(err:Error)
  115.                 {
  116.                     Alert.show("生成小图片出错.");
  117.                 }
  118.             }
  119.             //当load原图的时候出错的处理方法 
  120.             public function loadError(event:IOErrorEvent):void
  121.             {
  122.                 Alert.show("上载的图片源:"+Loader(event.target.loader).contentLoaderInfo.loaderURL.toString()+"不存在,无法生成小图");
  123.             }       

 

你可能感兴趣的:(xml,String,object,function,File,Matrix)