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

时间:2013-04-25 15:41:39

标签: iphone ios cordova

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

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

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

2 个答案:

答案 0 :(得分:3)

答案 1 :(得分: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);
}