2013-01-14 56 views
0

我希望替换spirte的图像。Greensocks Imageloader如何在加载图像之前检查图像是否存在

但在此之前,我想检查图像是否存在于目录中。有没有办法做到这一点?

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

如果您使用ImageLoader加载一个图像,您可以使用回调(事件)。 我用ImageLoaderVars类,因为aoutocomplete

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

尝试添加属性nameloaderVars,并使用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); 
}