I've got a code, which caches loaded resources (both images and swfs) and saves its bytes to flash SharedObject
:
var cache:SharedObject = SharedObject.getLocal('dataCache');
cache.data[url] = (loader.contentLoaderInfo as LoaderInfo).bytes;
When there's a query to load new resource, the code checks if the resource is in cache and then loads its bytes:
var loader:FlexLoader = new FlexLoader();
// handlers skipped
var lc:LoaderContext = new LoaderContext();
lc.applicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain);
if (cache && cache.data[url])
loader.loadBytes(cache.data[url], lc)
else
loader.load(new URLRequest(url), lc);
The problem: sometimes FlexLoader.loadBytes()
loads PNGs and JPGs as MovieClip. I.e. when I look into loader.content
on complete event I see MovieClip and in loader.contentLoaderInfo.contentType
there's "application/x-shockwave-flash". (Sometimes means that such a behaviuor is reproduced on most computers and only after ".sol" file exceeds 9 Mb.)
Does anyone know how to make FlexLoader
load proper content?
There's a hack to avoid such a behaviour, but it's quite ugly. Loaded MovieClip
has inside itself only one child - the desired Bitmap
, so I can check on resource type and extract graphic:
public function getAsBitmap(imgName:String):Bitmap
{
var res:Object = getResource(imgName);
if (res is MovieClip)
{
try
{
return (res as MovieClip).getChildAt(0) as Bitmap;
}
catch(e:*) {}
}
return res as Bitmap;
}