flex 4 - 等待加载完成而不冻结
我想知道是否可以实现任何逻辑来让图像完成加载而不冻结。
为了我的应用程序,我创建了一个类,它在构造函数中获取图像 url 并使用“load()”函数加载 mx.controls.Image。
请参阅下面的类
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;
}
}
}
现在,我从主应用程序中使用此类,如下所示
var img:SpriteImage = new SpriteImage(e.imageUrl);
img.prepare(); // just waits till image is loaded but freezes
var obj:DisplayObject = img;
现在问题是应用程序永远不会从prepare()方法中出来。它冻结了浏览器,我必须终止插件进程才能解冻应用程序。
这里的主要标准是我需要等待图像完成加载,以便我可以在后续步骤中访问图像。我该怎么做?请帮忙
I was wondering if it is possible to implement any logic to wiat for the image to complete load without freezing.
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.
See the class below
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;
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我看来,您正在使用 while(ready == -1) 自己创建冻结,为什么不禁用用户进一步执行“步骤”的功能,直到图像已加载?
您不必像现在一样同步等待图像加载。加载图像后,您将通过其加载程序调度的完整事件收到通知。然后您可以启用取决于正在加载的图像的任何内容。
一种直接的方法是将应用程序中导航控件的 enabled 属性绑定到布尔属性。例如
,在调用 onCompleteHandler 后,在 SpriteImage 中添加并将其设置为 true。您可以绑定到该属性的值。另一种方法是从您的类中调度一个事件来通知其他人图像已加载并且可能会发生一些事情:]
Blaze
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.
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
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
ProgressBar
可以让您的 UI 更加有趣,使其在加载图像时不会冻结。请参阅 ProgressBar 控件的 Adobe 文档。标题为创建 ProgressBar 控件的段落有一个示例,演示如何在加载图像时显示
ProgressBar
。A
ProgressBar
could spice up your UI in a way that it's not freezing when you load the image.Have a look at the Adobe docs for the ProgressBar control. The paragraph titled Creating a ProgressBar control has an example which shows how to display a
ProgressBar
while the image is being loaded.