直接上代码:
package
{
import com.sociodox.utils.Base64;
import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.system.LoaderContext;
import flash.utils.ByteArray;
/**
* ...
* 加载二进制格式图片的例子,当然可以使用URLLoader来进行而进行二进制加载,而这里提供了
* 另一种方法,这种方式比较适合校验码的加载
* 参考网址http://blog.sina.com.cn/s/blog_557bc46501017lgh.html
* @author aisajiajiao
*/
public class Main extends Sprite
{
public function Main()
{
init();
}
private function init():void
{
var captchaLc:LoaderContext = new LoaderContext();
captchaLc.requestedContentParent = stage;
//这个字符串实际上是一个验证码图片转换而来。你可以转换一张自己的图片来代替。但请注意删除所有回车!
//转换网址:http://www.motobit.com/util/base64-decoder-encoder.asp
var captchaStr:String = "iVBORw0KGgoAAAANSUhEUgAAAGMAAAAeCAIAAAC35TfdAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAWYElEQVRogb15aXBc13XmvW/fel/RO/aVAEkA3CVR1GLZWiLZkVWys9ixZ4mTVGaSccZVSc3UTOXHpOZfqsZVqYknmSlnc2yVFcmOJEoUSREkAZDEToBoLN1AA43eu9/rfq/feudHUyBIQh5nsnw/UNUH957tnnPeuffAXK4BPgVCFrJAU5TLm1mKp72dYYzAwWFACBUKZUVRvV4Xz7OHrvkXQL6WqTZKAUfEwXv2FVvMlywERoLef1pZxMEfsiqmC3OV+q5gePQkubaz7gx4Y/EEzwsQwoMrIYQYhm1tZRsNube3HccPd+g/NyDEAUAWQgeJOMR003hkpWlaDUW3LGQXaAAQhmGGqVnIJHEaQuznkYV/+9u/v/+DxGmPEHHbonVNnl289cHFv5+4fg3D8e7uPgx7lB3HsYGAp1arFwplhqFpmvr/MvYfhaYmy1qdowWW4lsU07LKTQ0AEBC4gytV3djN1Zc3i5lCjaMpgtQqjWxTFTGMwDAC+zmc9dAKCCGG4RTG6yqxvrs9v7owOX3jJz99O5VaP3Qzw9DhsF9V9a2tXVlW0MNn+y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NDp9r7wpTfVpmKaBgCQpGiW52x2p412BLvjGMRSi/csCPLBwMunn2rt8vmDjyiWr5aWs8nBYE8jv4GQ9VkmQAgJkkIIkRSNEHL5KbubU0VDLjdp8kF/QzAcyfNqKavkd5RClm+L/QM9BZCiNymcwiDscHgXijsNAzhw58aVT7xdneNf+dLU939w6lfffOBWRdF1w26zEyS5sZeOeEM8e//d8mDy252uM8++8Ijllmpu/3AVRSzMRsYHe9fmllxrORTvvX/XARZCCIM4AMA0NIgRlgLD8iDfdBTqNYoSwM9ES/qnf4ErKGzdLVRzdVfQ1qJDAmc8ofrWGkRArRQ+y1M/+7n60/kKQABAHSE7bk9P3Fj78BLvdnm72tO3ZvatBQA47A6fz8cxvMfuXkqvWMis6mXJEP+fs2VIQM/pUH2jBgDAcCza3WHDyMx6CiFkIn2zNLlTm9dNRdfVWiGDYVib3+dx2UsFsVLh0pvXLcv82fwPes0TttEsUdgSDxgJMZJs/Rt89qTj/wJ97I+QnP5DTQAAAABJRU5ErkJggg==";
//把字符串转换成ByteArray,Base64下载地址http://www.sociodox.com/base64.html
var captchBa:ByteArray = Base64.decode(captchaStr);
//把loader定义为局部变量,确保加载完成后及时释放内存,放置内存泄露
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadedHandler);
//通常这里是从远程加载一个二进制数据流,但为了简化代码,这里用captchaBa做为Mockup Data
loader.loadBytes(captchBa,captchaLc);
}
private function loadedHandler(e:Event):void
{
e.target.removeEventListener(Event.COMPLETE,loadedHandler);
//取出loader里面的内容,Loader类会自动进行解码所以占用一些内存,URLLoader则不会
var captchaBmp:Bitmap = e.target.content as Bitmap;
captchaBmp != null ? addChild(captchaBmp) : trace("Error: the format of captcha is wrong!");
e.target.loader.unloadAndStop(); //卸载loader,垃圾回收
}
}
}
天地会上有一篇和他相近的文章
http://bbs.9ria.com/forum.php?mod=viewthread&tid=82069