首页 文章

在PhoneGap IOS上缓存图像的最佳方法

提问于
浏览
4

我正在构建一个PhoneGap ios应用程序,用于使用JSON从服务器导入数据,这个数据包含图像URL,我已经用来缓存本地存储中的数据,以便在应用程序没有连接互联网时使用它但我有一个问题是什么是缓存图像的最佳方法 .

我正在考虑将图像转换为data-uri并将其保存到IOS DataBase .

如果此解决方案可行或是否有其他最佳解决方案,请提供建议?

2 回答

  • 3
  • 1

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

    这些文件将存储在App的Documents文件夹中,因此您需要找到该路径(与每次安装的路径不同)然后在本地保存文件并将路径保存在localstorage中 .

    这是一个代码片段 - 首先它制作并保存一个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);
    }
    

相关问题