as3 - 预加载 XML 图片库

发布于 2024-08-06 11:20:49 字数 115 浏览 5 评论 0原文

我有一个图像库类,可以循环加载缩略图和完整图像。然后我将其推入一个数组并使用每个位图创建一个影片剪辑。运作良好。

在这个过程的一部分中,我可以在显示整个图库之前预加载所有图像?

谢谢!

I have an image gallery class that loads thumbnails and full images one-by-one in a loop. Then I push then into an array and create one movie clip with each bitmap. It's working good.

In witch part of this process I can preload all images, before display the entire gallery?

Thanx!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淤浪 2024-08-13 11:20:49

就我个人而言,我非常喜欢这个令人敬畏的 BulkLoader 库:

  • 非常灵活
  • 它 提供有用的帮助程序来访问加载的资源(例如 getBitmap("asset_id") 返回一个类型化对象)
  • 它提供有意义的进度反馈(可以选择返回值以反映整体“权重”的进度,而不是单个文件的原始值大小可能有很大差异)
  • 它处理并发加载(这显然比顺序加载更快)。

或者,如果您想推出自己的解决方案,请在循环时将事件侦听器添加到

Event.COMPLETE

notification. In the handler for this event increment a counter; once this counter equals the number of elements you've loaded all your assets.

伪代码:

var loadCounter:int = 0;

var img_arr:Array = [
    "img1_thumb.jpg", "img1.jpg", 
    "img2_thumb.jpg", "img2.jpg", 
    ... 
];
var image_num:int = img_arr.length;

var ldr:Loader;
var req:URLRequest;
var path:String;
var i:int   = 0;
for(; i < image_num; i++) 
{
     path = "http://myserver.com/images/" + img_arr[i];
     req  = new URLRequest(path);
     ldr  = new Loader();
     ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, _onLoadComplete);
     ldr.load(req);
}

private function _onLoadComplete(event:Event):void
{
    if(++loadCounter == image_num)
    {
        // do whatever you need to your loaded assets
    }
}

Personally I'm a huge fan of the awesome BulkLoader library:

  • It's incredibly flexible
  • It provides useful helpers to access loaded assets (e.g. getBitmap("asset_id") returns a typed object)
  • It gives meaningful feedback on progress (values can optionally be returned to reflect progress by overall "weight" as opposed to the raw value from individual files that may vary significantly in size)
  • It handles concurrent loading (which is obviously faster than loading in sequence).

Alternatively, if you want to roll your own solution, as you loop through, add an event listener to the

Event.COMPLETE

notification. In the handler for this event increment a counter; once this counter equals the number of elements you've loaded all your assets.

Pseudo-code:

var loadCounter:int = 0;

var img_arr:Array = [
    "img1_thumb.jpg", "img1.jpg", 
    "img2_thumb.jpg", "img2.jpg", 
    ... 
];
var image_num:int = img_arr.length;

var ldr:Loader;
var req:URLRequest;
var path:String;
var i:int   = 0;
for(; i < image_num; i++) 
{
     path = "http://myserver.com/images/" + img_arr[i];
     req  = new URLRequest(path);
     ldr  = new Loader();
     ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, _onLoadComplete);
     ldr.load(req);
}

private function _onLoadComplete(event:Event):void
{
    if(++loadCounter == image_num)
    {
        // do whatever you need to your loaded assets
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文