html - iOS 13 Swift 5 wkwebview - 将文档目录中的图像显示到 wkwebview 真实设备中
问题描述
我有一个应用程序,其中下载的图像要在 WKWEBVIEW 中以本地 html 显示。一切正常,但它没有在 iPhone 设备上显示图像
html是
"\n
<img src="images/d35fb6a3-8a21-4196-9616-ad2c6db60669/fd21b894-38c0-42c5-aa69-a938abe40e4b2467857252325869136.png">
\n<img src="images/d35fb6a3-8a21-4196-9616-ad2c6db60669/c927a2a6-4ef0-481d-b27d-525cec7ed3814195490571216387044.png">
与此 HTML 匹配,它将显示在 wkwebview 中,该 wkwebview 使用以下代码在模拟器上完美显示
let baseURL = Functions.FileIO.contentFolderURL()
static func contentFolderURL() -> URL? {
guard let folderURL = DataManager.shared.applicationDocumentsURL?.appendingPathComponent("content") else {
return nil
}
do {
let properties = try (folderURL as NSURL).resourceValues(forKeys: [URLResourceKey.isDirectoryKey])
if let isDirectory = properties[URLResourceKey.isDirectoryKey] as? Bool , isDirectory == false {
return nil
}
} catch let error as NSError where error.code != NSFileReadNoSuchFileError {
return nil
} catch {
// No folder, so we create it.
do {
try FileManager.default.createDirectory(atPath: folderURL.path, withIntermediateDirectories: true, attributes: nil)
} catch {
return nil
}
}
return folderURL
}
然后最后显示
baseURL 应运而生
可选▿一些:file:///Users/paza/Library/Developer/CoreSimulator/Devices/D2204E03-8A8F-4EF4-8924-683CF519DD19/data/Containers/Data/Application/E4ED56CF-B247-4471-9F9B-23384FD6D6B3/Documents /content/ - _url:file:///Users/paza/Library/Developer/CoreSimulator/Devices/D2204E03-8A8F-4EF4-8924-683CF519DD19/data/Containers/Data/Application/E4ED56CF-B247-4471-9F9B-23384FD6D6B3 /文件/内容/
self.webView?.loadHTMLString(HTML, baseURL: baseURL)
在下面的模拟器中工作在设备中不起作用
如https://stackoverflow.com/a/52838445/3455426[]2 _ _
建议我尝试
self.webView?.loadFileURL(baseURL, allowingReadAccessTo: baseURL)
任何线索将不胜感激,在此先感谢
解决方案
在 WKWebview 中,如果我们必须显示带有缓存资源的 HTML,首先我们必须将 HTML 字符串保存在文档目录中的 HTML 文件中。
确保您的缓存资源路径和 HTML 文件的路径也相同,缓存资源仅由 lastPathComponent 标识。
所以错误是应该是的HTML
let HTML = "<html><body>\n<p><img src=\"fd21b894-38c0-42c5-aa69-a938abe40e4b2467857252325869136.png\"></p>\n<p><img src=\"c927a2a6-4ef0-481d-b27d-525cec7ed3814195490571216387044.png\"><br></p>\n</body></html>"
具体来说,最后一个路径组件应该在那里。
我也跟着 https://en.it1352.com/article/9f185d4d185243cc92128148443ca660.html 这里的解释几乎完美,我只是保存在文档目录中以便快速。
推荐阅读
- arrays - 如何在 Perl 中正确替换散列数组中的值?
- java - E/sqlite3_android:[IKR-38846] ONEPLUS_NAME_PARTS_MATCH SQLITE_OK
- ruby-on-rails - Ruby On Rails 设计使用用户名而不是电子邮件注册和登录
- flutter - 如何在我的 pubspec.yaml 配置中指定本地 git 存储库的分支?
- android - 如何在 Android Jetpack Compose 的 TabRow 中更改按下的 Tab 颜色?
- azure-devops - 如何查看 .NET 或 Python 代码的代码依赖关系?
- python - 从文本中丢弃回车和/或换行
- android - 这里可能有什么问题,我刚开始使用颤振,像两个成功的项目一样运行,但最近在尝试构建时遇到了这个错误
- docker - Docker 容器已安装卷,但 nginx 仍为 /var/www/html 提供服务
- matlab - 试图抓取图 MATLAB 的前 50 个点