WKWebView加载速度太慢

时间:2017-10-14 09:10:56

标签: ios swift wkwebview

我正在使用MarkDonwView库:https://github.com/keitaoouchi/MarkdownView

将MD字符串转换为显示为webview。渲染花费的时间太长,因为它会加载所有内容,然后显示视图。是否可以在显示文本正文时使图像加载异步,因此它看起来不会太慢?

这是加载html文件的方式。

 if bundle.bundleIdentifier?.hasPrefix("org.cocoapods") == true {
      htmlURL = bundle.url(forResource: "index",
                           withExtension: "html",
                           subdirectory: "MarkdownView.bundle")
    } else {
      htmlURL = bundle.url(forResource: "index",
                           withExtension: "html")
    }

    if let url = htmlURL {
      let templateRequest = URLRequest.init(url: url, cachePolicy: .returnCacheDataElseLoad, timeoutInterval: 60) //URLRequest(url: url)

      let escapedMarkdown = self.escape(markdown: markdown) ?? ""
      let imageOption = enableImage ? "true" : "false"
      let script = "window.showMarkdown('\(escapedMarkdown)', \(imageOption));"
      let userScript = WKUserScript(source: script, injectionTime: .atDocumentEnd, forMainFrameOnly: true)

      let controller = WKUserContentController()
      controller.addUserScript(userScript)

      let configuration = WKWebViewConfiguration()
      configuration.userContentController = controller

      let wv = WKWebView(frame: self.bounds, configuration: configuration)
      wv.scrollView.isScrollEnabled = self.isScrollEnabled
      wv.translatesAutoresizingMaskIntoConstraints = false
      wv.navigationDelegate = self
      addSubview(wv)
      wv.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
      wv.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
      wv.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
      wv.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
      wv.backgroundColor = self.backgroundColor

      self.webView = wv

      wv.load(templateRequest)
    } else {
      // TODO: raise error
    }

0 个答案:

没有答案