Flex 3: Image Caching/404 Handling
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;
}

Self Portrait Collage
Release Animals
100 Mile Journey
Boni from Trap Door Costume
Monster Cake!
Sinclair ZX Spectrum +2 Costume
Video Project for Rimmer’s Wedding
Vodafone Micro SIM iPhone 4 Confusion
Integrating AIR Application with iTunes
Custom Flex WordPress Frontend




Comments/Trackbacks