iOS 13 Swift 5 wkwebview-将文档目录中的图像显示到wkwebview真实设备中 [英] iOS 13 Swift 5 wkwebview - display image from documents directory into wkwebview real device

查看:285
本文介绍了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 ="图像/d35fb6a3-8a21-4196-9616-ad2c6db60669/c927a2a6-4ef0-481d-b27d-525cec7ed3814195490571216387044.png

与此HTML匹配,它将显示在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成为

可选▿一些: 文件:///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/Documents/内容/

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 在这里的解释几乎是完美的,我只是保存在documents目录中,以便迅速进行操作.

I have an app with downloaded images to be displayed in local html in WKWEBVIEW. Everything is working fine but it is not showing image on iPhone device

the html is

"\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">

\n"

matching to this HTML it is to be displayed in a wkwebview which gets displayed perfectly on simulator with the following code

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
        }

and then finally displaying

baseURL comes to be

Optional ▿ some : 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/Documents/content/

self.webView?.loadHTMLString(HTML, baseURL: baseURL)

working in simulator below does not work in Device

as https://stackoverflow.com/a/52838445/3455426[ ]2

suggested I tried

self.webView?.loadFileURL(baseURL, allowingReadAccessTo: baseURL)

but the result this comes blank on real device

any leads will be appreciated , Thanks in advance

解决方案

In WKWebview if we have to show HTML with Cached resources ,firstly we have to save the html string in HTML file in documents directory.

make sure ur cached resources path and the path of HTML file are same also , the cached resources are identified by the lastPathComponent only .

So the fault was the HTML which should have been

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>"

Specifically the last path component should be there .

Also I followed https://en.it1352.com/article/9f185d4d185243cc92128148443ca660.html here the explanation is almost perfect , I just saved in documents directory for swift.

这篇关于iOS 13 Swift 5 wkwebview-将文档目录中的图像显示到wkwebview真实设备中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆