Created
March 11, 2011 18:32
-
-
Save khopkins218/866330 to your computer and use it in GitHub Desktop.
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
/* | |
Developed by Kevin L. Hopkins (http://kevin.h-pk-ns.com) | |
You may borrow, steal, use this in any way you feel necessary but please | |
leave attribution to me as the source. If you feel especially grateful, | |
give me a linkback from your blog, a shoutout @Devneck on Twitter, or | |
my company profile @ http://wearefound.com. | |
/* Expects parameters of the directory name you wish to save it under, the url of the remote image, | |
and the Image View Object its being assigned to. */ | |
cachedImageView = function(imageDirectoryName, url, imageViewObject) | |
{ | |
// Grab the filename | |
var filename = url.split('/'); | |
filename = filename[filename.length - 1]; | |
// Try and get the file that has been previously cached | |
var file = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, imageDirectoryName, filename); | |
if (file.exists()) { | |
// If it has been cached, assign the local asset path to the image view object. | |
imageViewObject.image = file.nativePath; | |
} else { | |
// If it hasn't been cached, grab the directory it will be stored in. | |
var g = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, imageDirectoryName); | |
if (!g.exists()) { | |
// If the directory doesn't exist, make it | |
g.createDirectory(); | |
}; | |
// Create the HTTP client to download the asset. | |
var xhr = Ti.Network.createHTTPClient(); | |
xhr.onload = function() { | |
if (xhr.status == 200) { | |
// On successful load, take that image file we tried to grab before and | |
// save the remote image data to it. | |
file.write(xhr.responseData); | |
// Assign the local asset path to the image view object. | |
imageViewObject.image = file.nativePath; | |
}; | |
}; | |
// Issuing a GET request to the remote URL | |
xhr.open('GET', url); | |
// Finally, sending the request out. | |
xhr.send(); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment