2013-01-14 55 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); 
}