开发者

FlexLoader.loadBytes(): Bitmap is loaded as MovieClip

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;
}


Yeah, I have stumbled upon that problem too recently. Thing is the Loader class can be used as is to add its content to the scene because it loads a display object. It's not always convenient, however.

Use an URLLoader instead and set its data format as binary. You'll then be able to get the data correctly. If you absolutely need the FlexLoader, you can also loadBytes() the data from the URLLoader but I don't think that's necessary in your case.

private function loadFromDisk(url:String, loader:Loader):void
{
    var urlLoader:URLLoader = new URLLoader();
    var urlRequest:URLRequest = new URLRequest(url);

    urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
    urlLoader.addEventListener(Event.COMPLETE, loadComplete, false, 100, true);
    urlLoader.load(urlRequest);
}

private function loadComplete(event:Event):void
{
    var urlLoader:URLLoader = URLLoader(event.target);
    imageBytes:ByteArray = urlLoader.data; //here's your data!
    flexLoader.loadBytes(imageBytes); //useless here, just to show you it's possible
}


I wasn't using Shared object to store anything but textual data. BUT. Maybe you can find something useful here: Is it possible to store images in the SharedObject of Flash?

The pre last answer contains link to an example. It seems like the approach there a bit differs from yours. P.S. The link is broken, but thanks to Google, we have a saved copy ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜