chrisben / imgcache.js

JS library based on the File API to cache images for offline recovery (target: cordova/phonegap & chrome)
Other
826 stars 216 forks source link

Cache Image not found #72

Open rregue opened 10 years ago

rregue commented 10 years ago

I am building an iOS App with Phonegap 3.5 and Framework7 (http://www.idangero.us/framework7/). I have masonry view and I would like to cache the images for off-line use.

I am using localstorage to store a JSON object with an object like {"images": ["http://www.domain.com/media/i11_ma40ea7c6.jpg","http://www.domain.com/media/i11_ma4sdea7c6.jpg"]}

Is it possible to replace the path to the image in the localStorage object with the path to the cache Image?

When I download the JSON, I check if Image is cached, and it is not I cache it. Until here it works fine.

When I try to use in the template, or use ImgCachegetUrl() to replace the path in the json, it does not work.

Any suggestions?

chrisben commented 10 years ago

You can get the url of a cached image using ImgCache.getCachedFileURL. Please note that this is an asynchronous function, you will receive the cached url within the success callback, this method returns nothing. Your code might look like:

for (var i = 0; i < images.length; i++) {
   var image = images[i];
   ImgCache.getCachedFileUrl(image, function (img, newPath) {
     /* success callback */
     images[i] = newPath;
     mySaveToLocalStorageFunction(images); // to be done asynchronously as well
   }), function (img) {
     /* error callback */
     console.log('Failed to get path for image ' + img);
  });
}

Note also here that you need to save your modified array asynchronously as well, not just when the array for loop is complete.

chrisben commented 9 years ago

@rregue what's the status of this issue for you? Someone tried something similar over at #98, if that helps you in any way.

rregue commented 9 years ago

I tried getting it to work but I did not succeed and I did not spend more time on it. I will look into #98 to see if I can make it work this time. Thanks!