我在使用as3中的File API加载图像并在舞台上显示时遇到问题。我能够成功加载它,但当我把它放在舞台上时,图像就是噪音。我假设因为我需要以某种方式将PNG / JPG解码为位图数据并且我做错了吗?这是我的代码:
public function browseForIcon(){
var file:File = new File();
file.addEventListener(Event.SELECT, onFileSelected);
file.browseForOpen("Select a an image");
}
private function onFileSelected(event:Event):void {
var stream:FileStream = new FileStream();
stream.open(event.target as File, FileMode.READ);
var bytes:ByteArray = new ByteArray();
stream.readBytes(bytes);
var img = new BitmapData(160,160);
img.setPixels(new Rectangle(0,0,160,160),bytes);
this.addChild(new Bitmap(img));
}
}
谢谢!