zoukankan      html  css  js  c++  java
  • 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()))

  • 相关阅读:
    js 图片压缩
    C#.NET中对称和非对称加密、解密方法汇总--亲测可用
    最精美详尽的 HTTPS 原理图
    swagger ui
    python爬取百度谷歌搜索结果
    Gitlab 11.0.3 迁移简明笔记
    ELK stack 7.6.x + kafka构建日志平台
    Nginx 内存占用高排查
    运维机器人 hubot 集成 jenkins
    运维机器人hubot,解放运维的神器
  • 原文地址:https://www.cnblogs.com/destiLaugh/p/11202378.html
Copyright © 2011-2022 走看看