0

Rather than loading a WebView from a URL like this

override func viewDidLoad() {
    super.viewDidLoad()

    let url = URL(string: imageUrl.urlString)
    let request = URLRequest(url: url!)
    webView.load(request)

}

override func loadView() {
    let webConfiguration = WKWebViewConfiguration()
    webView = WKWebView(frame: .zero, configuration: webConfiguration)
    webView.uiDelegate = self

    view = webView
}

How can I load a UIImage onto the WebView in Swift 4?

Arasuvel
  • 2,971
  • 1
  • 25
  • 40
husharoonie
  • 431
  • 1
  • 4
  • 19
  • 1
    May we know the purpose behind that ? Is that for showing a placeholder image when content of webView is not available or is there something else ? – Nitish Apr 25 '18 at 11:07
  • https://stackoverflow.com/questions/46281889/loading-local-images-into-wkwebview/46395862 – andykkt Apr 25 '18 at 11:09
  • 2
    Possible duplicate of [Loading local images into WKWebView](https://stackoverflow.com/questions/46281889/loading-local-images-into-wkwebview) – rishi Apr 25 '18 at 11:22
  • Good job rishi. This is swift 4 i am asking. – husharoonie Apr 25 '18 at 11:24

1 Answers1

3

Try Something like this.

let html = "<html><img src=\"datadb/DestinationGuide/Images/YourImage.png\"/> </html>"

//Save this html in `DocumentDirectory`
let saveHTML = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("temp.html")

try? html.data(using: .utf8)!.write(to: saveHTML)

//Now load this `temp.html` file in webView
webView.loadRequest(URLRequest(url: saveHTML))
Chatar Veer Suthar
  • 15,541
  • 26
  • 90
  • 154