gpt4 book ai didi

swift - 在WebKitView中显示本 map 片

转载 作者:行者123 更新时间:2023-11-30 11:45:23 24 4
gpt4 key购买 nike

我需要在 WebKitView 中显示一些图片。这些图片存储在我的文档目录中。 WebKitView 内容是通过编程方式从(非常)长的字符串构建的。

我是这样调用它的:

@IBOutlet weak var descView: UIView!

var webView: WKWebView!
let localFile = NSHomeDirectory() + "/Documents/image.png"
let htmlString = "<img src=\"\(localFile)\" />" // for example

let webConfiguration = WKWebViewConfiguration()
webConfiguration.allowsAirPlayForMediaPlayback = true
webConfiguration.allowsInlineMediaPlayback = true
webConfiguration.allowsAirPlayForMediaPlayback = true
if #available(iOS 10.0, *) {
webConfiguration.dataDetectorTypes = .all
}

webView = WKWebView(frame: CGRect(x:0, y:0, width: descView.frame.width, height: descView.frame.height), configuration: webConfiguration)
webView.allowsLinkPreview = true
webView.navigationDelegate = self
webView.uiDelegate = self

let bundlePath = Bundle.main.bundlePath
let baseURL = NSURL.fileURL(withPath: bundlePath)

webView.loadHTMLString(htmlString, baseURL: baseURL)
descView.addSubview(webView)

但是图片不存在。

[编辑] 由于我使用 bundle 中的一些脚本,因此需要 baseURL。 scripts

htmlString 可能是:

let htmlHeader = "<!DOCTYPE html><html>\n" +
"<head><script type=\"text/javascript\" async src=\"MathJax-2.7.2/MathJax.js?config=TeX-MML-AM_CHTML\"></script></head>\n" +
"<body style=\"font-size:16pt; text-align:justify;\">"
let htmlFooter = "</body></html>"
let htmlString = htmlHeader + "<img src=\"\(localFile)\" /> $$a^2 + b^2 = c^2$$" + htmlFooter

这应该显示图片和一些 latex 文本。 latex 还可以,没有图片。

最佳答案

我猜问题是您的 localFile 路径和/或您的 baseURL 不正确。

  1. 您的应用程序是否在沙箱中运行?该图像确实位于 NSHomeDirectory() + "/Documents/image.png" 吗?您是否打印了要检查的路径?
  2. NSURL.fileURL(withPath: Bundle.main.bundlePath) 是正确的 baseURL 吗?您将从主目录加载图像,但从应用程序包加载 baseUrl 。这行不通

你尝试过吗

webView.loadHTMLString(htmlString, baseURL: nil)

let htmlString = "<img src=\"image.png\" />"
let baseURL = URL(fileURLWithPath: NSHomeDirectory() + "/Documents")
webView.loadHTMLString(htmlString, baseURL: baseURL)

另外,考虑使用真正的 HTML 字符串而不仅仅是图像标签,例如:

    let htmlString = "<!DOCTYPE html>" +
"<html>" +
"<head>" +
"<meta charset=\"UTF-8\">" +
"<style type=\"text/css\">" +
"img{position: absolute; top: 0; bottom: 0; left: 0; right: 0; margin: auto; max-width: 100%; max-height: 100%;}" +
"</style>" +
"</head>" +
"<body>" +
"<img src='\(localFile)'/>" +
"</body>" +
"</html>"

关于swift - 在WebKitView中显示本 map 片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48890249/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com