Apache flex as3:重新加载图像
我有一个图像,我正试图加载,然后重新加载。以下是我加载图像的代码:Apache flex as3:重新加载图像,apache-flex,actionscript-3,Apache Flex,Actionscript 3,我有一个图像,我正试图加载,然后重新加载。以下是我加载图像的代码: public function loadImage(url:String, _w:int, _h:int):void { this._stwidth = _w; this._stheight = _h; this._imageURL = url; if(!_imageURL) { return;
public function loadImage(url:String, _w:int, _h:int):void
{
this._stwidth = _w;
this._stheight = _h;
this._imageURL = url;
if(!_imageURL)
{
return;
}
this.alpha = 1.0; //need this because we might have just faded the image out
_ldr.alpha = 0.0;
_prog.alpha = 1.0;
_sqr.alpha = 0.0;
_sqr.graphics.clear();
if(_hasLoaded)
{
try
{
_ldr.close();
_ldr.unload();
}
catch(e:Error)
{
//trace("bmdisplay has loaded once, but there was an error: " + e.message);
}
}
_ldr.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
_ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
_ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
_ldr.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
_ldr.contentLoaderInfo.addEventListener(Event.INIT, onOpen);
_ldr.load(new URLRequest(_imageURL));
}
由于某些原因,此代码在第二次加载时不会在不发出错误的情况下加载图像。
有人能帮我弄清楚吗
我完全不明白为什么我的变量asLoaded会出错。
我有一个onComplete()处理程序,它将该变量设置为true,此后我从未将其设置为false
我不知道我还应该尝试什么
谢谢有时候我写了一个助手类来实现类似的功能。helper类扩展了Loader并提供了图像的自动缩放。下面是该类的代码:
package{
package {
import flash.display.Loader;
import flash.geom.Rectangle;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
public class ImageLoader extends Loader {
private var _imageURL:String; // URL of image
private var _imageBoundary:Rectangle; // boundary rectangle for the image
private var _loaded:Boolean; // flag which tells whether image is loaded or not.
private var _isLoading:Boolean; // flag which say if any loading is in progress
//Constructor function, which calls Loader's constructor
// and loads and resize the image
public function ImageLoader(url:String = null, rect:Rectangle = null):void {
super();
_imageURL = url;
_imageBoundary = rect;
_loaded = false;
_isLoading = false;
loadImage();
}
// sets the image for the loader and loads it
public function set imageURL(url:String):void {
_imageURL = url;
loadImage();
}
// sets the boundary of the image and resizes it
public function set boundary(rect:Rectangle):void {
_imageBoundary = rect;
resizeImage();
}
private function removeListeners():void {
this.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
this.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onError);
this.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
}
private function onComplete(e:Event):void {
_loaded = true;
_isLoading = false;
removeListeners();
resizeImage();
}
//In case of error, we are not propogating the event
private function onError(e:Event):void {
e.stopImmediatePropagation();
removeListeners();
}
// real loading goes here
// it first closes and unloads the loader and
// then loads the image
private function loadImage():void {
if (_isLoading) {
trace("Some loading is in progess");
return;
}
try {
this.close();
this.unload();
}
catch(e:Error) {
//discarded
}
if (!_imageURL)
return;
_loaded = false;
_isLoading = true;
this.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
this.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
this.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
this.load(new URLRequest(_imageURL));
}
// standard resizing function for image so that it's
// aspect ratio is maintained.
private function resizeImage():void {
if (!_imageBoundary || !_loaded)
return;
var aspect:Number = width / height;
var cAspect:Number = _imageBoundary.width / _imageBoundary.height;
if (aspect <= cAspect) {
this.height = _imageBoundary.height;
this.width = aspect * this.height;
}
else {
this.width = _imageBoundary.width;
this.height = this.width / aspect;
}
this.x = (_imageBoundary.width-this.width)/2 + _imageBoundary.x;
this.y = (_imageBoundary.height-this.height)/2 + _imageBoundary.y;
}
}
}
导入flash.display.Loader;
导入flash.geom.Rectangle;
导入flash.net.URLRequest;
导入flash.events.Event;
导入flash.events.IOErrorEvent;
导入flash.events.SecurityErrorEvent;
公共类ImageLoader扩展了加载程序{
私有变量_imageURL:String;//图像的URL
私有变量_imageBoundary:Rectangle;//图像的边界矩形
private var _loaded:Boolean;//指示是否加载图像的标志。
private var _isLoading:Boolean;//表示是否正在加载的标志
//构造函数,它调用加载器的构造函数
//并加载和调整图像大小
公共函数ImageLoader(url:String=null,rect:Rectangle=null):void{
超级();
_imageURL=url;
_imageBoundary=rect;
_加载=错误;
_isLoading=false;
loadImage();
}
//设置加载程序的图像并加载它
公共函数集imageURL(url:String):无效{
_imageURL=url;
loadImage();
}
//设置图像的边界并调整其大小
公共函数集边界(矩形:矩形):无效{
_imageBoundary=rect;
调整图像大小();
}
私有函数RemovelListeners():void{
this.contentLoaderInfo.removeEventListener(Event.COMPLETE,onComplete);
this.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,onError);
this.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY\u ERROR,onError);
}
完成的私有函数(e:事件):无效{
_加载=真;
_isLoading=false;
RemovelListeners();
调整图像大小();
}
//如果出现错误,我们不会发布该事件
私有函数onError(e:事件):无效{
e、 停止即时复制();
RemovelListeners();
}
//真正的负载在这里
//它首先关闭并卸载装载机,然后
//然后加载图像
私有函数loadImage():void{
如果(_isLoading){
跟踪(“某些加载正在进行”);
返回;
}
试一试{
这个。关闭();
这个。unload();
}
捕获(e:错误){
//丢弃
}
如果(!\u imageURL)
返回;
_加载=错误;
_isLoading=true;
this.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
this.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onError);
this.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY\u ERROR,onError);
加载(新的URL请求(_imageURL));
}
//图像的标准大小调整功能,以便
//保持纵横比。
私有函数resizeImage():void{
如果(!_图像边界| |!_已加载)
返回;
变量方面:数字=宽度/高度;
var cAspect:Number=\u imageBoundary.width/\u imageBoundary.height;
if(aspect有时我编写了一个帮助器类来实现类似的功能。帮助器类扩展了Loader并提供图像的自动缩放。下面是该类的代码:package{
package {
import flash.display.Loader;
import flash.geom.Rectangle;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
public class ImageLoader extends Loader {
private var _imageURL:String; // URL of image
private var _imageBoundary:Rectangle; // boundary rectangle for the image
private var _loaded:Boolean; // flag which tells whether image is loaded or not.
private var _isLoading:Boolean; // flag which say if any loading is in progress
//Constructor function, which calls Loader's constructor
// and loads and resize the image
public function ImageLoader(url:String = null, rect:Rectangle = null):void {
super();
_imageURL = url;
_imageBoundary = rect;
_loaded = false;
_isLoading = false;
loadImage();
}
// sets the image for the loader and loads it
public function set imageURL(url:String):void {
_imageURL = url;
loadImage();
}
// sets the boundary of the image and resizes it
public function set boundary(rect:Rectangle):void {
_imageBoundary = rect;
resizeImage();
}
private function removeListeners():void {
this.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
this.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onError);
this.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
}
private function onComplete(e:Event):void {
_loaded = true;
_isLoading = false;
removeListeners();
resizeImage();
}
//In case of error, we are not propogating the event
private function onError(e:Event):void {
e.stopImmediatePropagation();
removeListeners();
}
// real loading goes here
// it first closes and unloads the loader and
// then loads the image
private function loadImage():void {
if (_isLoading) {
trace("Some loading is in progess");
return;
}
try {
this.close();
this.unload();
}
catch(e:Error) {
//discarded
}
if (!_imageURL)
return;
_loaded = false;
_isLoading = true;
this.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
this.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
this.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
this.load(new URLRequest(_imageURL));
}
// standard resizing function for image so that it's
// aspect ratio is maintained.
private function resizeImage():void {
if (!_imageBoundary || !_loaded)
return;
var aspect:Number = width / height;
var cAspect:Number = _imageBoundary.width / _imageBoundary.height;
if (aspect <= cAspect) {
this.height = _imageBoundary.height;
this.width = aspect * this.height;
}
else {
this.width = _imageBoundary.width;
this.height = this.width / aspect;
}
this.x = (_imageBoundary.width-this.width)/2 + _imageBoundary.x;
this.y = (_imageBoundary.height-this.height)/2 + _imageBoundary.y;
}
}
}
导入flash.display.Loader;
导入flash.geom.Rectangle;
导入flash.net.URLRequest;
导入flash.events.Event;
导入flash.events.IOErrorEvent;
导入flash.events.SecurityErrorEvent;
公共类ImageLoader扩展了加载程序{
私有变量_imageURL:String;//图像的URL
私有变量_imageBoundary:Rectangle;//图像的边界矩形
private var _loaded:Boolean;//指示是否加载图像的标志。
private var _isLoading:Boolean;//表示是否正在加载的标志
//构造函数,它调用加载器的构造函数
//并加载和调整图像大小
公共函数ImageLoader(url:String=null,rect:Rectangle=null):void{
超级();
_imageURL=url;
_imageBoundary=rect;
_加载=错误;
_isLoading=false;
loadImage();
}
//设置加载程序的图像并加载它
公共函数集imageURL(url:String):无效{
_imageURL=url;
loadImage();
}
//设置图像的边界并调整其大小
公共函数集边界(矩形:矩形):无效{
_imageBoundary=rect;
调整图像大小();
}
私有函数RemovelListeners():void{
this.contentLoaderInfo.removeEventListener(Event.COMPLETE,onComplete);
this.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,onError);
this.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY\u ERROR,onError);
}
完成的私有函数(e:事件):无效{
_加载=真;
_isLoading=false;
RemovelListeners();
调整图像大小();
}
//如果出现错误,我们不会发布该事件
私有函数onError(e:事件):无效{
e、 停止即时复制();
RemovelListeners();
}
//真正的负载在这里
//它首先关闭并卸载装载机,然后
//然后加载图像
私有函数loadImage():void{
如果(_isLoading){
跟踪(“某些加载正在进行”);
返回;
}
试一试{
这个。关闭();
这个。unload();
}
捕获(e:错误){
//丢弃
}
public function loadImage(url:String, _w:int, _h:int):void
{
// do your job and die bravely, no need to be global
var _ldr:Loader = new Loader();
this._stwidth = _w;
this._stheight = _h;
this._imageURL = url;
if(!_imageURL)
{
return;
}
this.alpha = 1.0; //need this because we might have just faded the image out
// now you will need to make alpha = 1 on ldrHolder since _ldr is dead after this function
ldrHolder.alpha = 0.0;
_prog.alpha = 1.0;
_sqr.alpha = 0.0;
_sqr.graphics.clear();
// remove the old image, doesn't matter whether its empty or not
while(ldrHolder.numChildren > 0){
ldrHolder.removeChildAt(0);
}
//add image
ldrHolder.addChild(_ldr);
_ldr.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
_ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
_ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
_ldr.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);
_ldr.contentLoaderInfo.addEventListener(Event.INIT, onOpen);
_ldr.load(new URLRequest(_imageURL));
}