AS3:ByteArray 和 BitmapData
我正在做一个简单的测试。 我想将 BitmapData 写入 ByteArray。 我正在尝试使用 writeObject() 和 readObject() 来做到这一点。 读取对象似乎无法理解 BitmapData。
var byteArray : ByteArray = new ByteArray();
var _cache : BitmapData = new BitmapData( 640, 480, true, 0x000000 );
var _blank : BitmapData = new BitmapData( 640, 480, true, 0x000000 );
byteArray.writeObject( _blank );
byteArray.position = 0;
_cache = byteArray.readObject() as BitmapData;
trace( _cache ); // Traces null
谁能帮我解决这个问题吗? 我无法理解 readObject(); 出了什么问题。
我知道我可以执行 getPixels() 和 setPixels() 操作,但我想将对象视为此处的对象。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用
BitmapData::getPixels
和BitmapData::setPixels
...将类实例写入 ByteArray 因此永远不会完全按预期工作...尝试使用 Sprites .. :) ...这主要是因为默认实现仅将对象的属性写入IDataOutput
(ByteArray
或Socket) ...从这个意义上来说,像素不是 BitmapData 的属性...BitmapData 只是一个 ActionScript 包装器,它向某些内部公开一个接口flash播放器数据结构...
您应该为
BitmapData
编写一个适配器 ,实现IExternalizable
,这样你就可以控制发生的事情......本质上,你只需要读/写尺寸,以及实际的像素有效负载......也许是透明标志......记住使用registerClassAlias
...greetz
后退2dos
you should use
BitmapData::getPixels
andBitmapData::setPixels
... writing class instances to a ByteArray as such never completely works as expected ... try with Sprites ... :) ... this is mostly due to the fact, that the default implementation somehow only writes an object's property to theIDataOutput
(ByteArray
orSocket
) ... the pixels are not a property of theBitmapData
in that sense ... theBitmapData
is just an ActionScript wrapper, that exposes an interface to some internal flash player data structure ...you should write an adapter for
BitmapData
, that implementsIExternalizable
, so that you have control, over what happens ... essentially, you will only need to read/write the dimensions, and the actual pixel payload ... maybe the transparent-flag ... remember to useregisterClassAlias
...greetz
back2dos