的URLLoader数据的BitmapData [英] URLLoader data to BitmapData
本文介绍了的URLLoader数据的BitmapData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图加载图像文件,该文件旁边的.swf文件。事情是这样的:
VAR装载机:的URLLoader =新的URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(引发Event.COMPLETE,功能(五:事件):无效{
跟踪(typeof运算(loader.data));
图形= spritemap =新Spritemap(loader.data,32,32);
...
}
但是,这是我得到的输出:
对象
[错误]异常,信息=错误:无效的源图像。
的事情是loader.data有图像的字节,但是不是BitmapData实例,而这正是Spritemap期待。
如何转换成的BitmapData?
感谢
解决方案
//定义图片的网址
VAR URL:的URLRequest =新的URLRequest(http://sstatic.net/ads/img/careers2-ad-header-so.png);
//创建加载器和负载网址
VAR IMG:装载机=新的Loader();
img.load(URL);
//监听器图像加载完成
img.contentLoaderInfo.addEventListener(引发Event.COMPLETE,加载);
//附加的图像时加载完成
功能加载(五:事件):无效
{
VAR位:位图= e.target.content;
doStuffWithBitmapData(bitmap.bitmapData);
的addChild(位);
//删除监听器
e.target.removeEventListener(引发Event.COMPLETE,加载);
}
/ **
*手柄装的BitmapData
*参数骨密度加载的BitmapData
* /
功能doStuffWithBitmapData(BMD:的BitmapData):无效
{
跟踪(BMD);
//你的code
}
基本上;
您应该使用装载机
,不是的URLLoader
。您可以访问的的BitmapData code>加载的
位图
与 bitmap.bitmapData code>。
I'm trying to load an image file that's right next to the .SWF file. Something like this:
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, function(e:Event):void {
trace(typeof(loader.data));
graphic = spritemap = new Spritemap(loader.data, 32, 32);
...
}
But this is the output I get:
object
[Fault] exception, information=Error: Invalid source image.
The thing is loader.data has the image's bytes but is not a BitmapData instance and that's what Spritemap is expecting.
How to convert to BitmapData?
Thanks
解决方案
// define image url
var url:URLRequest = new URLRequest("http://sstatic.net/ads/img/careers2-ad-header-so.png");
// create Loader and load url
var img:Loader = new Loader();
img.load(url);
// listener for image load complete
img.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
// attaches the image when load is complete
function loaded(e:Event):void
{
var bitmap:Bitmap = e.target.content;
doStuffWithBitmapData(bitmap.bitmapData);
addChild(bitmap);
// remove listener
e.target.removeEventListener(Event.COMPLETE, loaded);
}
/**
* Handle loaded BitmapData
* @param bmd The loaded BitmapData
*/
function doStuffWithBitmapData(bmd:BitmapData):void
{
trace(bmd);
// your code
}
Basically;
You should be using Loader
, not URLLoader
. You can access the BitmapData
of the loaded Bitmap
with bitmap.bitmapData
.
这篇关于的URLLoader数据的BitmapData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文