SharedObject对象可以缓存二进制数据,可以将图片或者swf文件转化成byteArray缓存到本地,见下面的源码:
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.PixelSnapping;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.geom.Rectangle;
import flash.net.SharedObject;
import flash.utils.ByteArray;
public class SharedObjectByteArr extends Sprite
{
[Embed(source="../images/1.jpg")]
private var imgClass:Class;
private var sharedname:String = "showPicwky";
public function SharedObjectByteArr()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
this.cash();
}
private function cash():void
{
var imgShared:SharedObject = SharedObject.getLocal(sharedname);
if(imgShared.data.byte == null)
{
trace("ok1 : "+imgShared.data.byte+" : "+imgShared.data.ifAdd);
var bmp:Bitmap = new imgClass();
var byte:ByteArray = bmp.bitmapData.getPixels(bmp.bitmapData.rect);
imgShared.data.byte = byte;
imgShared.data.width = bmp.bitmapData.rect.width;
imgShared.data.height = bmp.bitmapData.rect.height;
imgShared.flush();
}
else
{
var imgByte:ByteArray = imgShared.data.byte;
var rect:Rectangle = new Rectangle(0,0,imgShared.data.width,imgShared.data.height);
trace(rect.x+" : "+rect.y+" : "+rect.width+" : "+rect.height);
var bmd:BitmapData = new BitmapData(rect.width,rect.height,true,0x00ffffff);
bmd.setPixels(rect,imgByte);
var bmp:Bitmap = new Bitmap(bmd,PixelSnapping.AUTO,true);
this.addChild(bmp);
}
}
}
}