FLEX 图片拷贝

  在用FLEX做GIS相关的开发的时候,遇到一个问题。因为是监控类的系统,所以需要要求地图上的ICON的实时更新,从而会出现重复加载的情况。就是重复请求相同的图片用做背景,尤其是在加载的ICON较多的时候,会有大量的http请求占用大量的资源。

  这时候想到两种解决办法:

  • 把图片资源嵌入FLEX文件
  • 把图片做缓存

  (1)把图片资源嵌入FLEX文件:

[Bindable]
[Embed(source="image/example.jpg")]
 private var imgClass:Class;

但是当图片特别多的时候,例如有几百张的话,就不堪重负了。那我们看第二种。

    (2)大家都知道ActionScript的对象都是引用传递。假如需要做缓存的话必须做一个副本用于拷贝,否则就会出现一张图片被多个元素使用的情况,造成的结果就是”有的孩子没饭吃“。这样就需要用到图片的拷贝。废话不多说先写个拷贝的功能类:

package com.charles.util
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.geom.Rectangle;
    import flash.utils.ByteArray;
    import mx.core.UIComponent; 
    
    public class BitmapCopyHepler 
    { 
        public function BitmapCopyHepler () 
        { 
            super(); 
        } 
        /**
         * 将可视的UIComponent组件转换为ByteArray数组
         *@param bitMapData:BitmapData 
         *@param bitmap:Bitmap
         */
        public static function BitmapDataToByteArray(bitMapData:BitmapData,bitmap:Bitmap):ByteArray{ 
            //读取指定像素区域生成一个ByteArray
            var pixels:ByteArray = bitMapData.getPixels( new Rectangle(0,0,bitmap.width,bitmap.height) ); 
            //将数据源的高和宽一起存储到数组中                              
            pixels.writeShort(bitmap.width); 
            pixels.writeShort(bitmap.height); 
            return pixels; 
        } 

       /**
        * 将Byte数组转为Bitmap对象
        *@param byArr:ByteArray 
        *@param bitmapData:BitmapData
        */
        public static function ByteArrayToBitmap(byArr:ByteArray,bitmapData:BitmapData):Bitmap{ 
            if(byArr==null){ 
                return null; 
            } 
            //读取出存入时图片的高和宽
            var bmd:ByteArray= byArr; 
            bmd.position=bmd.length-2; 
            var imageWidth:int = bmd.readShort(); 
            bmd.position=bmd.length-4; 
            var imageHeight:int= bmd.readShort(); 
            //拷贝BitmapData数据
            var copyBmp:BitmapData = bitmapData.clone();
            //给图片中的每一个像素赋值,按照原有的图片结构重新绘制图片
            bmd.position = 0; 
            copyBmp.setPixels(new Rectangle(0,0,imageWidth,imageHeight),bmd);
            var bmp:Bitmap = new Bitmap(copyBmp ); 
            return bmp; 
        } 
    } 
}            

好的下面就可以用这个功能类来拷贝图片了,具体的操作本人就不一一赘述啦,希望各位大神指正:

...
var imgPathString = "../images/example.jpg";
var loader:Loader = new Loader();
            if(imgPathString)
                loader.load(new URLRequest(imgPathString));
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
...


     /**
         * 监听到Loader加载完毕以后执行的函数
         */
        private function loadComplete(e:Event):void {
            var loader:Loader=Loader(e.target.loader);//得到Loader对象
            var icon:DisplayObject = Bitmap(loader.content);//Loader里面的内容
            //获取Bitmap对象的BitmapData数据
            var bitmapData:BitmapData = Bitmap(loader.content).bitmapData;
            //转为byteArray.
            var bytes: ByteArray = BitmapBytes.BitmapDataToByteArray( Bitmap(loader.content).bitmapData,Bitmap(loader.content));
...
            
        }
    

转载请注明出处:http://www.cnblogs.com/xinwang/p/4283166.html

原文地址:https://www.cnblogs.com/xinwang/p/4283166.html