【问题标题】:Best way to cache images on PhoneGap IOS在 PhoneGap IOS 上缓存图像的最佳方法
【发布时间】:2013-04-25 15:41:39
【问题描述】:

我正在构建一个 PhoneGap ios 应用程序,该应用程序用于使用 JSON 从服务器导入数据,该数据包含图像 URL,我已经用于将数据缓存在本地存储中,以便在应用程序无法上网时使用它连接,但我有一个担心是什么是缓存图像的最佳方式。

我正在考虑将图像转换为 data-uri 并将其保存到 IOS 数据库。

请告知此解决方案是否可行或是否有其他最佳解决方案?

【问题讨论】:

    标签: iphone ios cordova


    【解决方案1】:
    【解决方案2】:

    Phonegap API 有一个文件系统,您可以使用它来存储从远程服务器下载的图像,这可能是您的最佳选择吗?

    这些文件将存储在应用程序的 Documents 文件夹中,因此您需要找到该路径(每次安装到下一次都不同),然后将文件保存在本地并将路径保存在 localstorage 中。

    这是一个代码 sn-p- 首先它创建并保存一个 dummy.html 文件以测试本地路径 - 然后它下载文件 -

    function downloadFile(webURL,webFilename){
        window.requestFileSystem(
                                 LocalFileSystem.PERSISTENT, 0,
                                 function onFileSystemSuccess(fileSystem) {
                                 fileSystem.root.getFile(
                                                         "dummy.html", {create: true, exclusive: false},
                                                         function gotFileEntry(fileEntry){
                                                         var sPath = fileEntry.fullPath.replace("dummy.html","");
                                                         var fileTransfer = new FileTransfer();
                                                         fileEntry.remove();
    
                                                         fileTransfer.onprogress = function(result){
                                                         var percent =  result.loaded / result.total * 100;
                                                         percent = Math.round(percent);
                                                         console.log('Downloaded:  ' + percent + '%');
                                                         };
    
                                                         fileTransfer.download(
                                                                               webURL,
                                                                               sPath + webFilename,
                                                                               function(theFile) {
                                                                               console.log("download complete: " + theFile.toURL());
                                                                               showLink(theFile.toURL());
                                                                               },
                                                                               function(error) {
                                                                               console.log("download error source " + error.source);
                                                                               console.log("download error target " + error.target);
                                                                               console.log("upload error code: " + error.code);
                                                                               navigator.notification.alert('Seems to be an error downloading this background. Try again later.', null, 'Error', 'OK');
                                                                               }
                                                                               );
                                                         },
                                                         fail);
                                 },
                                 fail);
    
    }
    
    function showLink(localurl){
        console.log(localurl);
    }
    

    【讨论】:

      猜你喜欢
      • 2012-06-19
      • 2012-07-15
      • 2013-10-17
      • 2011-01-16
      • 2011-01-27
      • 2011-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多