0

    [AS3]as3.0图片上传时进行本地预览的源代码示例

    2023.06.06 | admin | 129次围观

    在Flash 9之前,含Flash 9,Flash无法实现本地图片预览的功能。Flash 10添加了FileReference.load方法,使得本地图片预览成了可能。使用流程:

    FileReference.browse(),FileReference.load(),以及Loader.loadBytes(FileReference.data),具体使用略过。

    这种使用流程存在一个问题,

    遇到大图片时,本地预览会存在比较严重的性能问题,预览一张3.5M的图片,内存升到60M,预览15张3M左右大小的图片时,内存飙升到500M,可能会引起用户浏览器崩溃。

    给出了解决方案。大致的思想是,在Loader加载到图片数据后,做如下处理:

    1. //make the image 50 px in height 
    2. var scale:Number=50/loader.height; 
    3. //and actually resize it 
    4. loader.width*=scale; 
    5. loader.height*=scale; 
    6. loader.width=Math.round(loader.width); 
    7. loader.height=Math.round(loader.height); 
    8. //use a container so we don't need a matrix in BitmapData.draw 
    9. var container:Sprite=new Sprite(); 
    10. container.addChild(loader); 
    11. //take the snapshot 
    12. var bmpData:BitmapData=new BitmapData(container.width,container.height); 
    13. bmpData.draw(container); 
    14. var bmp:Bitmap=new Bitmap(bmpData);  

    这样,相当于把图片按比例缩放之后,获取位图数据,转换为Bitmap对象图片上传网站时候的代码怎么做图片上传网站时候的代码怎么做,这样,只需要把Bitmap对象添加到Container中即可实现图片预览。

    上述方法只在Flash Player自身运行时有效,对于浏览器中加载swf预览本地图片,还需要做一个操作:

    1. (loader.contentLoaderInfo.content as Bitmap).bitmapData.dispose();//从内存中删除原图的bitmapData引用 
    2. loader.unload(); 

    版权声明

    本文仅代表作者观点。
    本文系作者授权发表,未经许可,不得转载。

    发表评论