Hi,
I know that SDK 1.8.0 automatically caches remote images (in Library/Caches) once, so next time when this image is requested from a server, the cached file will be displayed automatically. Works even if user is offline.
Now I upgrade to lastes SDK 2.1.2 and although the images are still stored in mentioned directory, the caching is ignored? In offline mode no previously loaded image will be shown and in online mode all images will be loaded again from the server, ignoring the existing cached file.
Why this new behaviour? It works so fine in 1.8.0 and now I have to implement an own caching logic?
Best Danny
2 Answers
You can use this code logic:
exports.create = function(imageDirectoryName, url, imageViewObject, hires) { var filename = url.split('/'); var hiresfilename; filename = filename[filename.length - 1]; hiresfilename = filename.split('.'); hiresfilename = hiresfilename[hiresfilename.length - 2] + '@2x' + hiresfilename[hiresfilename.length - 1]; var file = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, imageDirectoryName, filename); var hiresfile = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, imageDirectoryName, hiresfilename); if (file.exists()) { imageViewObject.image = file.nativePath; } else { var g = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, imageDirectoryName); if (!g.exists()) { g.createDirectory(); }; var xhr = Ti.Network.createHTTPClient(); xhr.onload = function() { if (xhr.status == 200) { file.write(xhr.responseData); file.remoteBackup = false; if (hires) { hiresfile.write(xhr.responseData); hiresfile.remoteBackup = false; } imageViewObject.image = file.nativePath; }; }; xhr.open('GET', url); xhr.send(); }; };
I think I found the bug why image caching is not working.
Just comment out these lines in ImageLoader.m:
if (hires && [TiUtils isRetinaDisplay]) { // Save as @2x w/retina ... }So e.g. instead of 1cf1835516fab2213ec5675f01865ad3@2x.jpg the remote image will be saved as 1cf1835516fab2213ec5675f01865ad3.jpg
There is no need for a @2x suffix, the cached image will still be display in retina resolution and ImageView will detect this local file.
Any custom caching solution with xhr is too slow and not asynchron. I guess implemented native caching is based on ASICache?
Tested with iOS 6, both retina and non-retina device, works fine ;)
Your Answer
Think you can help? Login to answer this question!