WKWebview加载本地图片时出现路径问题

出现问题情况是:将uiwebview换成wkwebview之后,之前将webview上那些图片放回本地下载后缓存的图片没办法在webview上找到,最后排查出原因是,wkwebview需要将图片和网页文件放在同个路径下。

具体实现方法如下(获取图片缓存的路径,将本地html和css文件复制到该路径下):

css复制到图片路径:

       let baseDir = ImageCacheTool.imageBasePath()!//本地图片缓存路径

            if !FileManager.default.fileExists(atPath: baseDir) {

                try? FileManager.default.createDirectory(atPath: baseDir, withIntermediateDirectories: true, attributes: nil)

            }

       let srcPath = Bundle.main.path(forResource: "webview", ofType: "css")

          if !FileManager.default.fileExists(atPath: desPath) {

              try? FileManager.default.copyItem(atPath: srcPath ?? "", toPath: baseDir + "/webview.css")

          }

html复制到图片路径

       let htmlPath = ImageCacheTool.imageBasePath() + "/index.html"

          try? html.write(toFile: htmlPath, atomically: true, encoding: .utf8)

          webView.loadFileURL(URL(fileURLWithPath: htmlPath), allowingReadAccessTo: URL(fileURLWithPath: ImageCacheTool.imageBasePath()))

 

转载于:https://www.cnblogs.com/destiLaugh/p/11202378.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值