[]将字节数组转为图片
flash air端将图片通过ByteArray保存为字节数组,并存为img.byte
layaAir端加载了img.byte文件后如何还原为图片?flash端的loader有个loadBytes方法可以加载字节数组,但是layaAir端没有相关方法实现
layaAir端加载了img.byte文件后如何还原为图片?flash端的loader有个loadBytes方法可以加载字节数组,但是layaAir端没有相关方法实现
没有找到相关结果
已邀请:
要回复问题请先登录
2 个回复
cuixueying
赞同来自:
18688961270
赞同来自:
1.flash air端代码:
var file:File = File.applicationDirectory;
var picPath:String = file.resolvePath("img.png").nativePath;//获取图片路径
//读取图片文件到字节数组
var readFile:File = new File(picPath);
var readByte:ByteArray = new ByteArray();
var readStream:FileStream = new FileStream();
readStream.open(readFile, FileMode.READ);
readStream.readBytes(readByte, 0, readStream.bytesAvailable);
readStream.close();
//将读取的字节数组保存为img.byte文件
var savePath:String = File.applicationDirectory.resolvePath("img.byte").nativePath;//保存路径
var writeFile:File = new File(savePath);
var writeStream:FileStream = new FileStream();
writeStream.open(writeFile, FileMode.WRITE);
writeStream.writeBytes(readByte, 0, readByte.length);
writeStream.close();
2.layaAir端代码:
var url:String = "res/img.byte";
Laya.loader.load(url, Handler.create(this, byteLoadComplete,[url]),null,Loader.BUFFER);
private function byteLoadComplete(url:String):void {
var buffer:ArrayBuffer = Laya.loader.getRes(url);
var byte:Byte = new Byte(buffer);
byte.writeArrayBuffer(buffer, 4);
var blob:Object = new Browser.window.Blob([byte.buffer], { type:"image/png" } );
var url:String = Browser.window.URL.createObjectURL(blob);
var sp:Sprite = new Sprite();
sp.loadImage(url);
addChild(sp);
sp.pos(100, 200);
trace("url=" + url);//blob:file%3A///1c4effc4-4022-4ab5-8feb-e39ff76b7410
trace(sp.width);//0
/**
* 其他调试信息打印:
* [warn]Retry to load: blob:file%3A///1c4effc4-4022-4ab5-8feb-e39ff76b7410
[error]Failed to load: blob:file%3A///1c4effc4-4022-4ab5-8feb-e39ff76b7410
*/
}