Preload all images with $ ImageCacheFactory

I created a mobile application using an ionic frame. It contains many images. I need to download all the images without flickering. So I used $ImageCacheFactoryto preload all the images by specifying this blog .

I used below code. The problem is that the application contains 100 pngimages, so I need to transfer all png files.

.run(function($ImageCacheFactory) {
    $ImageCacheFactory.Cache([
        "img/user.png", 
        "img/profile.png",
        "img/logo.png",
        "img/splash.png", 
        "img/map.png", 
        "img/shop.png",
        "img/country.png", 
        "img/place.png"
      ]).then(function(){
         console.log("Images done loading!");
      },function(failed){
          console.log("Error..!!!");
    });
})

Is there any simple way to link to all png images using a single line code (all images are in a folder www/img). thanks

+4
2

angular factory

.factory("$fileFactory", function($q) {

  var File = function() {};

  File.prototype = {

    getEntries: function(path) {
      var deferred = $q.defer();
      window.resolveLocalFileSystemURI(path, function(fileSystem) {
        var directoryReader = fileSystem.createReader();
        directoryReader.readEntries(function(entries) {
          deferred.resolve(entries);
        }, function(error) {
          deferred.reject(error);
        });
      }, function(error) {
        deferred.reject(error);
      });
      return deferred.promise;
    }

  };

  return File;

});

, getEntries()

.run(function($ImageCacheFactory, $ionicPlatform, $fileFactory ) {
  var fs = new $fileFactory();
  $ionicPlatform.ready(function() {
    fs.getEntries('img').then(function(result) {
      var files = result;
      files = files.unshift({
        name: "[parent]"
      }).map(function(i, v) {
        return 'img/' + v.name;
      });
      $ImageCacheFactory.Cache(files).then(function() {
        console.log("Images done loading!");
      }, function(failed) {
        console.log("Error..!!!");
      });
    })
  });
});

Apache Cordova

cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-file.git

:

+3

, n. , .

-

. -, . , . .

0

Source: https://habr.com/ru/post/1605886/


All Articles