在项目中,在加载图片比较多的时候。如果将这些图片压缩成zip格式,一次性加载出来,然后解压处理是不是很好。
as3有很多第三方类库。其中有一个FZip就可以处理当前的这种环境。不仅可以处理jpg图片,还能处理gif动画图片。
FZip类库下载地址:https://github.com/claus/fzip/
用FZip将打包好的zip文件load进来。load完成时,有Complete事件通知。在Complete事件中,对数据进行分配处理。
记得注意一点,对想打包的图片。全选后打包成zip。不是对文件夹进行打包。
Demo:
这里文档类,包裹使用方法:
/** * load fzip source: https://github.com/claus/fzip/ */ package com.vini123 { import com.vini123.data.ImageData; import com.vini123.module.ImageList; import com.vini123.module.Loading; import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.Event; import flash.events.IOErrorEvent; import flash.events.ProgressEvent; import flash.net.URLRequest; import deng.fzip.FZip; import deng.fzip.FZipFile; [SWF(width = "640" , height="640" , frameRate="30" , backgroundColor ="#ffff00")] public class FZipDemo extends Sprite { private var host:String; private var url:String; private var imageList:ImageList; private var loading:Loading; public function FZipDemo() { if(stage) { addToStageHandler(); } else { addEventListener(Event.ADDED_TO_STAGE , addToStageHandler); } } private function addToStageHandler(e:Event = null):void { if(e) { removeEventListener(Event.ADDED_TO_STAGE , addToStageHandler); } stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT; this.graphics.beginFill(0xffff00); this.graphics.drawRect(0 , 0 , stage.stageWidth , stage.stageHeight); this.graphics.endFill(); initialize(); } private function initialize():void { loading = new Loading(resize); addChild(loading); imageList = new ImageList(); addChild(imageList); host = "assets/"; url = "roomCover.zip"; url = host + url; load(url); } private function load(value:String):void { var fzip:FZip = new FZip(); fzip.addEventListener(Event.COMPLETE , completeHandler); fzip.addEventListener(IOErrorEvent.IO_ERROR , ioErrorHandler); fzip.addEventListener(ProgressEvent.PROGRESS , progressHandler); fzip.load(new URLRequest(value)); } private function completeHandler(e:Event):void { var fzip:FZip = e.target as FZip; var len:int = fzip.getFileCount(); for(var i:int = 0 ; i< len ; i++) { var fzipFile:FZipFile = fzip.getFileAt(i) ImageData.getInstance().addDataByKey(fzipFile.filename , fzipFile.content); imageList.creatImage(fzipFile.filename); } imageList.resize(); loading.renderEnder(); } private function progressHandler(e:ProgressEvent):void { var bytesTotal:int = e.bytesTotal; var bytesLoaded:int = e.bytesLoaded; loading.render(bytesLoaded/bytesTotal); trace(bytesTotal , bytesLoaded); } private function ioErrorHandler(e:IOErrorEvent):void { trace("the zip url is error"); } private function resize():void { imageList.x = (stage.stageHeight - imageList.width) * 0.5; imageList.y = (stage.stageHeight - imageList.height) * 0.5; imageList.move(); } } }