在PhoneGap IOS上caching图像的最佳方法
我正在构build一个PhoneGap ios应用程序,用于使用JSON从服务器导入数据,这些数据包含图像URL,我已经使用它来caching本地存储中的数据,以便在应用程序脱离Internet连接时使用它,但是我有一个关心是什么是caching图像的最佳方式。
我正在考虑将图像转换为data-uri并将其保存到IOS数据库。
请咨询,如果这个解决scheme是可行的,还是有任何其他最好的解决scheme?
这个博客文章应该设置你在正确的轨道https://www.raymondcamden.com/2012/01/19/Downloading-files-to-a-PhoneGap-application-Part-1/
Phonegap API有一个文件系统,您可以使用它来存储从远程服务器下载的图像,这可能是您的最佳select?
这些文件将存储在应用程序的文档文件夹,所以你需要find该path(这是不同的安装到下一个),然后保存在本地文件,并保存path在本地存储。
这里是一个代码片段 – 首先它制作并保存一个dummy.html文件,以便锻炼本地path – 然后下载文件 –
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); }