Greensocks Imageloader如何在加载图像之前检查图像是否存在

时间:2013-01-14 03:41:37

标签: actionscript-3 greensock

我想替换一个spirte的形象。

但在此之前,我想检查图像是否存在于目录中。有办法吗?

newImage = new ImageLoader(dir + temChild.category + '/' + temChild.productUrl + '.png', {
                                                                    container : temChild,
                                                                    height : 80,
                                                                    width : 80,
                                                                    scaleMode : 'proportionalInside',
                                                                    onComplete : onColorImageLoad, 
                                                                    centerRegistration : true,
                                                                    noCache: true,
                                                                    autoDispose : true
                                                                  });

2 个答案:

答案 0 :(得分:2)

如果使用ImageLoader加载一个图像,则可以使用回调(事件)。 由于aoutocomplete

,我使用ImageLoaderVars类
var loaderVars:ImageLoaderVars = new ImageLoaderVars();
loaderVars.container=temChild;
loaderVars.height=80;
loaderVars.width=80;
loaderVars.scaleMode='proportionalInside';
loaderVars.centerRegistration=true;
loaderVars.noCache=true;
loaderVars.autoDispose=true;
loaderVars.onComplete=temChild;
loaderVars.onComplete=onColorImageLoad;

//lets watch for errors if we don't have file on the server or something else
loaderVars.onError=onErrorCallback;
loaderVars.onFail=onFailCallback;                                                        

newImage = new ImageLoader(dir+temChild.category+'/'+temChild.productUrl+'.png',
loaderVars);

private function onFailCallback(event:LoaderEvent):void
{
 //do something if you want here
}

private function onErrorCallback(event:LoaderEvent):void
{
 //do something if you want here
}

如果一个LoaderMax中有许多ImageLoders,则决定你想从失败的加载器中得到什么,默认情况下在LoaderMax中使用skipFailed = true;。还有LoaderMax事件:childFail,ioError,securityError。

答案 1 :(得分:1)

尝试将属性name添加到loaderVars并使用LoaderMax.getContent方法:

import com.greensock.loading.ImageLoader;
import com.greensock.loading.LoaderMax;
import com.greensock.events.LoaderEvent;

var loaderVars:Object = {
    container : this,
    height : 80,
    width : 80,
    scaleMode : 'proportionalInside',
    onComplete : onColorImageLoad, 
    centerRegistration : true,
    noCache: true,
    autoDispose : true,
    name: 'photo1' // Put your image id here
};

var newImage:ImageLoader = new ImageLoader('http://images.apple.com/es/home/images/hero_ipad_retina.jpg', loaderVars);

// Loads if photo1 has not been loaded
if (LoaderMax.getContent('photo1') && !LoaderMax.getContent('photo1').rawContent)
{
    newImage.load();
}

function onColorImageLoad (event:LoaderEvent):void 
{
    trace(event);
}