CODE:import index.base.net.ImageLoader;
源代码:
var il:ImageLoader = new ImageLoader;
il.load("http://www.xiaos8.com/uploads/200804/02_230327_1.jpg");
il.addEventListener(Event.COMPLETE,fun);
function fun(e:Event){
addChild(new Bitmap(il.data));
trace(il.loader)
trace(il.url)
}
CODE:package index.base.net{
import flash.display.Loader;
import flash.display.BitmapData;
import flash.net.URLRequest;
import flash.utils.ByteArray;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.events.EventDispatcher;
import flash.system.LoaderContext;
public class ImageLoader extends EventDispatcher{
public var url:String;
public var loader:Loader;
public var data:BitmapData;
//构造函数
public function ImageLoader(obj:Object = null,lc:LoaderContext = null) {
if(obj != null){
if(obj is ByteArray){
loadBytes(obj as ByteArray,lc);
}else if(obj is String){
load(obj as String,lc);
}else{
throw new Error("参数错误,构造函数第一参数只接受ByteArray或String");
}
}
}
//加载
public function load(_url:String,lc:LoaderContext = null):void{
url = _url;
loader = new Loader;
loader.load(new URLRequest(url),lc);
addEvent();
}
//加载字节
public function loadBytes(bytes:ByteArray,lc:LoaderContext = null):void{
loader = new Loader;
loader.loadBytes(bytes,lc);
addEvent();
}
//开始侦听
private function addEvent():void{
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,progressFun);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeFun);
}
//结束侦听
private function delEvent():void{
loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,progressFun);
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,completeFun);
}
//加载成功,发布成功事件
private function completeFun(e:Event):void {
data = loader.content["bitmapData"];
delEvent();
dispatchEvent(e);
}
//加载过程
private function progressFun(e:ProgressEvent):void{
dispatchEvent(e);
}
//清除
public function clear():void{
loader.unload();
loader = null;
data = null;
}
}
}
IT时代网(关注微信公众号ITtime2000,定时推送,互动有福利惊喜)所有原创文章版权所有,未经授权,转载必究。
创客100创投基金成立于2015年,直通硅谷,专注于TMT领域早期项目投资。LP均来自政府、互联网IT、传媒知名企业和个人。创客100创投基金对IT、通信、互联网、IP等有着自己独特眼光和丰富的资源。决策快、投资快是创客100基金最显著的特点。
小何
小何
小何
小何