Created
September 26, 2011 23:07
-
-
Save pec1985/1243697 to your computer and use it in GitHub Desktop.
Cache Remote Images
This file contains hidden or 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 Utils = { | |
RemoteImage: function(a){ | |
a = a || {}; | |
var md5; | |
var needsToSave = false; | |
var savedFile; | |
if(a.image){ | |
md5 = Ti.Utils.md5HexDigest(a.image)+'.jpg'; | |
savedFile = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,md5); | |
if(savedFile.exists()){ | |
a.image = savedFile; | |
} else { | |
needsToSave = true; | |
} | |
} | |
var image = Ti.UI.createImageView(a); | |
if(needsToSave == true){ | |
function saveImage(e){ | |
image.removeEventListener('load',saveImage); | |
savedFile.write( | |
Ti.UI.createImageView({image:image.image,width:'auto',height:'auto'}).toImage() | |
); | |
}; | |
image.addEventListener('load',saveImage); | |
} | |
return image; | |
} | |
} | |
// Example: | |
var image = Utils.RemoteImage({ | |
image:'http://lalalal.com/image.jpg', | |
width:300, | |
height:200, | |
top:20 | |
}); | |
win.add(image); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I made a new gist with a working version of this code, for more general usage that works with callbacks.
https://gist.github.com/decklord/a2284b915dcbf0f6307f
Best Regards.