简体   繁体   English

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

[英]Best way to cache images on PhoneGap IOS

I'm building a PhoneGap ios app that used to import data from the server using JSON, this data contains image URL, I have already used to cache the data in the local storage to use it when the application is out of internet connection but I have one concern is about what is the best way to cache images. 我正在构建一个PhoneGap ios应用程序,用于使用JSON从服务器导入数据,这个数据包含图像URL,我已经用来缓存本地存储中的数据,以便在应用程序没有连接互联网时使用它但我有一个问题是什么是缓存图像的最佳方法。

I was thinking of converting the images to data-uri and save it to IOS DataBase. 我正在考虑将图像转换为data-uri并将其保存到IOS DataBase。

Please advice if this solution is doable or is there any other best solutions? 如果此解决方案可行或是否有其他最佳解决方案,请提供建议?

The Phonegap API has a file system that you can use to store images downloaded from remote servers, that might be your best option ? Phonegap API有一个文件系统,您可以使用它来存储从远程服务器下载的图像,这可能是您的最佳选择?

The files would be stored in the App's Documents folder, so you would need to find that path (which is different from each install to the next) then save the file locally and save the path in localstorage. 这些文件将存储在App的Documents文件夹中,因此您需要找到该路径(与每次安装的路径不同)然后在本地保存文件并将路径保存在localstorage中。

Here is a code snippet- Firstly it makes and saves a dummy.html file in order to workout the local path - then it downloads the file - 这是一个代码片段 - 首先它制作并保存一个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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM