Created
April 23, 2011 02:14
-
-
Save dawsontoth/938172 to your computer and use it in GitHub Desktop.
Simple remote on demand image caching.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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