【问题标题】:Open image in webview (when image and css store in a local storage)在 webview 中打开图像(当图像和 css 存储在本地存储中时)
【发布时间】:2017-04-21 04:51:01
【问题描述】:

我可以从 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代码并替换srchref

已经尝试过了:

~/Documents/datadb/DestinationGuide/Images/Destination-Guide.png
/Documents/datadb/DestinationGuide/Images/Destination-Guide.png
/DestinationGuide/Images/Destination-Guide.png
/DestinationGuide/Images/Destination-Guide.png

【问题讨论】:

  • 我可以从 sqlite 数据库中以 String 格式获取数据。我没有 html 文件。每次用户登录时下载数据库和图像文件夹并放入文件夹。
  • @HarshilKotecha 字符串格式的意思是像Documents/datadb/DestinationGuide/Images/Destination-Guide.png这样的URL,对吧?
  • 不,我可以获得完整的字符串格式的 html 代码,而不仅仅是 URL
  • 当我在打印这样的日志输出后传递 html 时。

标签: ios swift webview


【解决方案1】:

您可以将此 html 保存在 DocumentDirectoryHTML 文件,然后加载该文件 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))

【讨论】:

  • 我也这么想,只是一分钟我测试了这个:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多