Pay for Hesitation: [BulkLoader] Failed to get content with url as type: [class Bitmap]

Pages

2011年4月13日 星期三

[BulkLoader] Failed to get content with url as type: [class Bitmap]

今天試玩Google image search API, 用bulkloader來載圖,
在flash builder上run的好好的,但放在網路就無法執行。
後來trace bulkloader的ImageItem.as source code



才發現bulkloader在發生cross domain security error時,並不會把err丟出來,反而是把載好圖的loader丟給上層。
回去翻文件,才發現其實文件有寫... XD


SecurityError

BulkLoader can handle SecurityErrorEvents. By listening to BulkLoader.SECURITY_ERROR("securityError"), client code can gracefully handle security error events. Listeners can be added to each LoadingItem subtype or to the entire BulkLoader object. Please note that if you add an event listener for a LoadingItem but not for BulkLoader, in case the event fires, it will bubble up and result in a unhandled security error. In order to make sure the error never goes unhandled, add a securityError handler to the BulkLoader instance.
This mechanism will catch events thrown on
.load operations. Event thrown when trying to access content (such as getBitmapData) is still left to client code to handle.
When using type=image or type=movieclip (which are loaded using a Loader instance), if, when the asset is loaded, accessing it's loader.content throws an error, then BulkLoader will gracefully recover and use a simple Loader instance. This way, you can still add a Loader object to the stage, but won't be able to run code inside of it (for MovieClip instances) or load it as data (transforming to BitmapData instances).
In this case, you can get the Loader instance either using getContent or getDisplayObjecLoader .


所以原本寫的
addChild( imageLoader.getBitmap( key ) );

改寫成
addChild( imageLoader.get( key ).content );

就不會有cross domain security 討人厭的問題了.

Ref:
http://groups.google.com/group/bulkloader-users/browse_thread/thread/31189ad472fb1d90

沒有留言: