自动加载图片类
算是接着第一篇内容的衍生吧
上次说了简单的根据字节流进行判断类型的,这次贴出工作中用到的一个类
由于工作的需求是需要分开SVG与常规图片的分别,所以这里就简单判断了非常规图片类型的都是SVG
其中的ImageType是一个外部静态类,自行添加就好
这里读到bytearray之后,处理的方式有很多,博主这里也只有举例了一种
而SVG是直接拿了 https://github.com/chndyyang/AS3SVGRenderer 的代码进行编译的
package { import com.lorentz.SVG.display.SVGDocument; import com.lorentz.SVG.events.SVGEvent; import com.lorentz.processing.ProcessExecutor; import flash.display.DisplayObject; import flash.display.Loader; import flash.display.LoaderInfo; import flash.display.Sprite; import flash.events.Event; import flash.events.IOErrorEvent; import flash.net.URLLoader; import flash.net.URLLoaderDataFormat; import flash.net.URLRequest; import flash.utils.ByteArray; public class LoadImage extends Sprite { private var _url:String; private var _loader:URLLoader; private var _byLoader:Loader; private var _isNeedAll:Boolean; private var _needW:uint; private var _needH:uint; public var data:Object; public function LoadImage(needWidth:uint = 0, needHeight:uint = 0, url:String = '', isNeedAllResize:Boolean = false) { super(); if(url != '') _url = url; if(needWidth != 0) _needW = needWidth; if(needHeight != 0) _needH = needHeight; _isNeedAll = isNeedAllResize; _loader = new URLLoader(); _loader.dataFormat = URLLoaderDataFormat.BINARY; } public function load(url:String=''):void { if(url != '') _url = url; if(_url == '') { trace("load image error url is null"); return; } _loader.addEventListener(Event.COMPLETE, loadCompleteHandler); _loader.addEventListener(IOErrorEvent.IO_ERROR, loadErrorHandler); try { _loader.load(new URLRequest(_url)); } catch(error:Error) { trace("load url error"); } } private function loadCompleteHandler(event:Event):void { if(_loader == null) return; var ba:ByteArray = _loader.data as ByteArray; if(ba == null) return; var type:String = getFileType(ba); _byLoader = new Loader(); switch(type) { case ImageType.BMP: case ImageType.GIF: case ImageType.PNG: case ImageType.JPG: { _byLoader.loadBytes(ba); _byLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, byLoadComHandler); break; } case ImageType.SVG: { ProcessExecutor.instance.initialize(stage); var svg:SVGDocument = new SVGDocument(); svg.parse(ba.readUTFBytes(ba.length)); svg.addEventListener(SVGEvent.RENDERED, function onRendered(event:SVGEvent):void{ resizeDS(event.target as DisplayObject); }); addChild(svg); break; } default: { break; } } } private function byLoadComHandler(event:Event):void { var info:LoaderInfo = event.target as LoaderInfo; resizeDS(info.content); addChild(info.content); _loader.removeEventListener(Event.COMPLETE, loadCompleteHandler); _loader.removeEventListener(IOErrorEvent.IO_ERROR, loadErrorHandler); _byLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, byLoadComHandler); } private function getFileType(fileData : ByteArray) : String { var b0 : int = fileData.readUnsignedByte(); var b1 : int = fileData.readUnsignedByte(); fileData.position = 0; if(b0 == 66 && b1 == 77) { return ImageType.BMP; }else if(b0 == 255 && b1 == 216) { return ImageType.JPG; }else if(b0 == 137 && b1 == 80) { return ImageType.PNG; }else if(b0 == 71 && b1 == 73) { return ImageType.GIF; }else if(b0 == 60 && b1 == 63){ return ImageType.SVG; } return ImageType.UNKNOW; } private function loadErrorHandler(event:IOErrorEvent):void { trace("load image error"); } private function resizeDS(ds:DisplayObject):void { var temp:uint; if(_needH != 0){ temp = ds.height; ds.height = _needH; if(!_isNeedAll){ ds.width = Math.round(ds.height / temp * ds.width); }else{ ds.width = _needW; } }else if(_needW != 0){ temp = ds.width; ds.width = _needW; if(!_isNeedAll){ ds.height = Math.round(ds.width / temp * ds.height); }else{ ds.height = _needH; } } } } }