flex 4 - 等待加载完成而不冻结 [英] flex 4 - wait for load complete without freezing

查看:132
本文介绍了flex 4 - 等待加载完成而不冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能实现任何逻辑来为图像完成加载而不会冻结。

I was wondering if it is possible to implement any logic to wiat for the image to complete load without freezing.

为了我的应用,我创建了一个在构造函数中获取图像url的类,并使用load()函数加载mx.controls.Image。

For the sake of my app, I created a class which takes image url in constructor and loads the mx.controls.Image using the "load()" function.

请参阅下面的类

package com
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.*;
//import flash.net.URLRequest;

import mx.controls.Alert;
import mx.controls.Image;
//import mx.utils.OnDemandEventDispatcher;

public class SpriteImage extends Sprite
{
    //Pass the source path or url here.
    private var imageUrl:String;
    private var ready:int = -1;
    private var img:Image;

    public function SpriteImage(imgUrl:String)
    {
        //imageUrl = imgUrl;
        loadImage(imgUrl);
    }
    private function loadImage(imgUrl:String):void
    {

        img = new Image();
        img.addEventListener(Event.COMPLETE,onCompleteHandler);
        img.addEventListener(IOErrorEvent.IO_ERROR,onErrorHandler);     
        //img.source = imgUrl;
        img.load(imgUrl);

    }

    private function onCompleteHandler(e:Event):void{

        img.x = -(img.width/2);
        img.y = -(img.height/2);
        this.addChild(img);
        ready = 1;
    }

    private function onErrorHandler(e:IOErrorEvent):void
    {
        ready = 0;
        //Alert.show("Can't load :" + e.toString());
    }

    public function prepare():Boolean{

        while(ready == -1){
            trace(0);
        }           
        return ready;
    }
}

}

现在,我在这个主应用程序中使用这个类

Now, I use this class from main app like this

var img:SpriteImage = new SpriteImage(e.imageUrl);
img.prepare(); // just waits till image is loaded but freezes
var obj:DisplayObject = img;

现在的问题是应用程序永远不会出现在prepare()方法中。它冻结了浏览器,我必须终止插件进程才能取消应用程序。

Now the problem is that the app never comes out of the prepare() method. It freezes the browser and I have to kill the plugin process to unfreez the app.

这里的主要标准是我需要等待图像完成加载,以便我可以在接下来的步骤中访问该图像。我怎样才能做到这一点。?请帮助

The main criteria here is that I need to wait for the image to complete loading so that I can access the image in the next steps. How can I do this.? Please help

推荐答案

在我看来,你正在用同时创建冻结(ready == -1) )为什么不禁用用户在步骤中更进一步的功能,直到加载图像为止?

Looks to me like you're creating the freeze yourself with that while(ready == -1) Why don't you just disable the ability for a user to go further in your "steps" until the image is loaded?

您不必同步等待图像加载现在的方式。加载图像后,您将通过其加载程序调度的完整事件获得通知。然后你可以启用任何取决于正在加载的图像。

You don't have to synchronously wait for the image to load the way you do now. Once the image is loaded, you'll get the notification by the complete event that its loader dispatches. Then you can enable whatever depends on that image being loaded.

一种直接的方法是绑定你的导航控件的启用属性应用于布尔属性。例如,在你的SpriteImage中添加

One straight forward way is to bind the enabled property of the navigation controls in your application to a boolean property. For example in your SpriteImage add

[Bindable] public var isImageLoaded:Boolean = false;

并在调用onCompleteHandler后将其设置为true。您可以绑定到该属性的值。另一种方法是从您的班级发送一个事件,通知其他人图片已加载,并且可能发生一些事情:]

and set it to true once the onCompleteHandler is called. You can bind to that property's value. Another way is to dispatch an event from your class to notify others that the image is loaded and that something can happen :]

Blaze

这篇关于flex 4 - 等待加载完成而不冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆