Flash On

Flex 3: Image Caching/404 Handling

17-08-2009

Sometimes it may be useful to cache images locally or provide default alternatives in situations where an AIR application is used offline. When linking to images at a URL always add an IOErrorEvent listener to the image, so that it is caught….

myImage.addEventListener(IOErrorEvent.IO_ERROR, Application.application.img_error);

Then rather than showing a broken image, a better and local alternative can be shown in its place….

public function img_error(evt:IOErrorEvent):void {
var defaultImage:String = File.applicationDirectory.resolvePath(”images/default.jpg”).nativePath;
}

If the AIR application is launched by the browser, then its fair to assume that the application is likely to be online when first launched, this gives you the opportunity to cache images for use later when the application has more patchy connectivity.

Take a look at this CacheImage class, which can be used when the application is online to cache images and then use them later….

var imageCacheFile:File = File.applicationStorageDirectory.resolvePath(”imageCache/” + imageName);
if (imageCacheFile.exists) {
myImage.source = imageCacheFile.nativePath;
} else {
var imageCache:CacheImage = new CacheImage(imageSource, imageName, imageCacheFile, defaultImage);
myImage.source = imageSource;
}

Written By Tim for the Web Technology section Tags: , , ,

Comments/Trackbacks

  1. No comments yet.
  1. No trackbacks yet.

Name (required)

E-Mail (required)

Website