我正在尝试将Sprite对象保存为我正在处理的设备上的文件,它似乎正常工作。我遇到的问题是将已保存的文件读回并将其作为精灵放回舞台上。下面是我到目前为止的代码,有人能告诉我它是做错了吗?我怀疑保存的不是我预期的,因为文件大小已经低于千字节。
public function save_album(e:Event):void
{
var outFile:File = File.documentsDirectory; // dest folder is desktop
outFile = outFile.resolvePath("canvas3.bin");
var fs:FileStream = new FileStream();
var bytes:ByteArray = new ByteArray();
//trace (File.documentsDirectory.url + "/canvas2.bin");
fs.open(outFile, FileMode.WRITE);
bytes.writeObject(graffitiContainer) //graffitiContainer is a Sprite
bytes.position = 0;
fs.writeBytes(bytes, 0, bytes.length);
fs.close();
}
public function open_album(e:Event):void
{
var inBytes:ByteArray = new ByteArray();
var inFile:File = File.documentsDirectory;
inFile = inFile.resolvePath("canvas3.bin"); // name of file to read
var inStream:FileStream = new FileStream();
inStream.open(inFile, FileMode.READ);
inStream.readBytes(inBytes, 0, inBytes.length);
inStream.close();
inBytes.position = 0;
ui.removeChild(graffitiContainer);
var obj:Sprite = inBytes.readObject() as Sprite; //returns a null
graffitiContainer = obj;
ui = new UIComponent();
graffitiContainer.x = 0;
graffitiContainer.y = 100;
ui.addChild(graffitiContainer);
}
答案 0 :(得分:0)
不完全确定我理解你想要完成的事情;但是,这个实现没有你想到的 - writeObject只能序列化一般的公共属性,而不是图形成员。
您可以将其渲染为位图。
看到关于此的博文: http://jacwright.com/201/serializing-display-objects/