Skip to content

Instantly share code, notes, and snippets.

@pec1985
Created September 26, 2011 23:07
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save pec1985/1243697 to your computer and use it in GitHub Desktop.
Save pec1985/1243697 to your computer and use it in GitHub Desktop.
Cache Remote Images
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);
@decklord
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment