AS3跨域加载图片时设置

跨域加载外部图片除需要设置crossdomain外,还需要在loader.load()方法添加context参数,

文档显示context参数为LoaderContext 类:

load(request:URLRequest, context:LoaderContext = null):void

When loading images (JPEG, GIF, or PNG) instead of SWF files, there is no need to specify a SecurityDomain or an application domain, because those concepts are meaningful only for SWF files. Instead, you have only one decision to make: do you need programmatic access to the pixels of the loaded image? If so, see the checkPolicyFile property.

所以,跨域加载正确的写法为:

import flash.system.LoaderContext;

var lc:LoaderContext = new LoaderContext(true);

var loader:Loader = new Loader();

var request:URLRequest = new URLRequest(url);

loader.load(request, lc);

否则,flash player安全沙箱会报“checkPolicyFile ”错误。 这个疏忽出现在了Papervision3D中。

使用BitmapFileMaterial时,如果使用的是跨域图片进行贴图,尽管设置了crossdomain.xml,仍然会报沙箱错误,解决方法是修改loadNextBitmap方法:

// LOAD NEXT BITMAP

private function loadNextBitmap():void {

// Retrieve next filename in queue

var file:String = _waitingBitmaps[0];

var request:URLRequest = new URLRequest( file );

var lc:LoaderContext = new LoaderContext(true);

var bitmapLoader:Loader = new Loader();

bitmapLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, loadBitmapCompleteHandler ); try {

// Load bitmap
bitmapLoader.load( request, lc );
// Save original
url _loaderUrls[ bitmapLoader ] = file;
// Busy loading
_loadingIdle = false;
Papervision3D.log(
BitmapFileMaterial: Loading bitmap from ;
+ file );
}
catch( error:Error ) {
// Remove from queue
_waitingBitmaps.shift();
// Loading
finished _loadingIdle = true;
Papervision3D.log( [ERROR] BitmapFileMaterial: Unable to load file + error.message ); }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值