Created
March 6, 2012 20:57
-
-
Save jonalter/1988931 to your computer and use it in GitHub Desktop.
iOS/Android: permanently caching imageView
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
App.UI.Image = function(_params) { | |
_params.preventDefaultImage = true; | |
var folder = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, "cache"); | |
if(!folder.exists()){ | |
folder.createDirectory(); | |
} | |
var filename = Ti.Utils.base64encode(_params.image.toString()); | |
// Android can't copy build with files over 100 in lenght http://jira.appcelerator.org/browse/TIMOB-6495' | |
if(filename.length > 99){ | |
filename = new String(filename).slice(filename.length - 99); | |
} | |
var file = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, "cache" + Ti.Filesystem.separator + filename); | |
if(file.exists()) { | |
_params.image = file.nativePath; | |
} else { | |
App.UI.ImageCache(_params.image); | |
} | |
return Ti.UI.createImageView(_params); | |
}; | |
App.UI.ImageCache = function(_location) { | |
var httpc = Ti.Network.createHTTPClient(); | |
httpc.onload = function() { | |
if(httpc.status == 200) { | |
var filename = Ti.Utils.base64encode(_location); | |
if(filename.length > 99){ | |
filename = new String(filename).slice(filename.length - 99); | |
} | |
var file = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, "cache" + Ti.Filesystem.separator + filename); | |
file.write(httpc.responseData); | |
} | |
}; | |
httpc.open("GET", _location); | |
httpc.send(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment