Cordova + Cordova-iOS 6.1.0-加载本地图像 [英] Cordova + cordova-ios 6.1.0 - load local image
问题描述
在使用cordova-ios 6.x之前,我有一个Cordova应用程序运行良好.
I have a cordova app that worked great prior to using cordova-ios 6.x.
此应用程序将几张图像下载到cordova.file.dataDirectory中以供离线访问....到目前为止,除了使用本地Web服务器插件外,我还找不到其他方法来显示此图像.
This app downloads couple of images into cordova.file.dataDirectory for offline access.... so far I haven't been able to find a way to display this image other than using local webserver plugin.
是否没有其他内置方式如何允许从cdvfile方案提供内容?[我需要设置一个img src],因为可能有很多图像,所以转换为base64对我来说不是一种选择.
Isn't there any other built-in way how to allow serve content from the cdvfile scheme ? [i need to set an img src] as there may be many images, converting to base64 is not an option for me.
任何帮助表示赞赏
推荐答案
适合像我一样挣扎的任何人.有一个解决方案,不需要对代码进行重大更改.
For anyone struggling as I was. There is a solution, which requires no significant change to the code.
需要2个步骤:
首先使用以下命令更新您的config.xml
First update your config.xml with following
<platform name="ios">
<preference name="scheme" value="app" />
<preference name="hostname" value="localhost" />
</platform>
然后使用未公开的方法转换file://链接
Then convert your file:// link by using the undocumented method
window.WkWebView.convertFilePath(filePath)
此方法执行到虚拟本地链接的转换,该链接使文件可访问并绕过WkWebView限制.这样的样本会更长一些
This method performs the conversion into a virtual localhost link that makes the file accessible and bypasses the WkWebView restrictions. A little bit longer sample goes like this
let localFile = cordova.file.dataDirectory + 'logo.png';
let convertedPath = window.WkWebView.convertFilePath(localFile);
document.getElementById("myImg").src = convertedPath;
这篇关于Cordova + Cordova-iOS 6.1.0-加载本地图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!