我可以从 sqlite 数据库中获取 html 数据。
let html = "<htmll>
...
...
<img src="Documents/datadb/DestinationGuide/Images/Destination-Guide.png">
...
</html>"
webView.loadHTMLString(html, baseURL: nil)
图片存放在这个路径
Documents/datadb/DestinationGuide/Images/Destination-Guide.png
它无法在 webview 中加载。
我想我想在添加这个之后给出完整的路径,比如应用程序名称。
如何扫描html代码并替换src
和href
已经试过了:
~/Documents/datadb/DestinationGuide/Images/Destination-Guide.png
/Documents/datadb/DestinationGuide/Images/Destination-Guide.png
/DestinationGuide/Images/Destination-Guide.png
/DestinationGuide/Images/Destination-Guide.png
最佳答案
您可以将此 html 保存在 DocumentDirectory
中作为 HTML
文件,然后加载该文件 webView
。尝试这样的事情。
let html = "<html><img src=\"datadb/DestinationGuide/Images/Destination-Guide.png\"/> </html>"
//Save this html in `DocumentDirectory`
let saveHTML = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("sample.html")
try? html.data(using: .utf8)!.write(to: saveHTML)
//Now load this `sample.html` file in webView
webView.loadRequest(URLRequest(url: saveHTML))
关于ios - 在 webview 中打开图像(当图像和 css 存储在本地存储中时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43534178/