Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
bluebill1049

Greensocks Imageloader how to check if a image exist before loading the image

Recommended Posts

I would like like to replace a spirte's image.

But before that, i want to check if the image exist in the directory or not. Is there a way to do it?

 

 

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

});

Link to post
Share on other sites

The only way to know if an asset is there is to attempt to load it.

You can listen for a number of load-related errors using an ImageLoader.

 

Consider the following example which attempts to load an image that doesn't exist:

 

import com.greensock.loading.*;
import com.greensock.events.LoaderEvent;

var img:ImageLoader = new ImageLoader("image.jpg", {
onFail:onFail,
onerror:onerror,
onIOError: onIOError});

function onFail(e:LoaderEvent){
trace("onFail");
}

function onerror(e:LoaderEvent){
trace("onerror");
}

function onIOError(e:LoaderEvent){
trace("onIOError");
}

img.load();

 

Will output the following info:

 

 

onIOError
----
Error on ImageLoader 'loader0' (image.jpg): Error #2035: URL Not Found. URL: file:////Volumes/Macintosh%20HD/Users/cschooff/Downloads/bezierDemo/image.jpg
----
onerror
onFail

 

All three errors fire in this scenario. An onIOError should be all you need though.

 

Other than that, I don't know what to suggest to detect the presence of an image.

 

Keep in mind that ImageLoader's support an alternateURL which will automatically load a "fallback" image in case of failuer:

 

 

alternateURL : String - If you define an alternateURL, the loader will initially try to load from its original url and if it fails, it will automatically (and permanently) change the loader's url to the alternateURL and try again. Think of it as a fallback or backup url. It is perfectly acceptable to use the same alternateURL for multiple loaders (maybe a default image for various ImageLoaders for example).

 

http://api.greensock...mageLoader.html

  • Like 1
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×