Skip to content

Instantly share code, notes, and snippets.

@dawsontoth
Created April 23, 2011 02:14
Show Gist options
  • Save dawsontoth/938172 to your computer and use it in GitHub Desktop.
Save dawsontoth/938172 to your computer and use it in GitHub Desktop.
Simple remote on demand image caching.
var iconStore = Ti.Filesystem.applicationDataDirectory + '/CachedRemoteImages';
var dir = Ti.Filesystem.getFile(iconStore);
if (!dir.exists()) {
dir.createDirectory();
}
function cacheRemoteURL(image, imageURL) {
if (imageURL) {
var hashedSource = Ti.Utils.md5HexDigest(imageURL + '') + '.' + imageURL.split('.').pop();
var localIcon = Ti.Filesystem.getFile(iconStore, hashedSource);
if (localIcon.exists()) {
image.image = localIcon.nativePath;
}
else {
image.image = imageURL;
image.addEventListener('load', function() {
localIcon.write(image.toImage());
});
}
}
}
var win = Ti.UI.createWindow();
var image = Ti.UI.createImageView();
cacheRemoteURL(image, 'http://www.appcelerator.com/wp-content/themes/appcelerator/img/APPC_logo.png');
win.add(image);
win.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment