There is a WebView , which loads the HTML from the server. It has <img> tags that contain a shortcut to the image, for example, <img src="/upload/images/1.jpg" /> .

When loading this HTML, images are not displayed (only frame). Obviously, it’s not possible to upload these images due to a short link.

How to make the pictures loaded in the WebView for the full link, for example, http://www.example.com/upload/images/1.jpg ?

Found download options from local files, but this option is not suitable.

  • Can you drop the sample code and the url on which it breaks? Most likely a slash at the beginning of the extra. <img src="upload/images/1.jpg" /> - Gralex
  • Is the problem reproduced only in webview? if you open in the browser everything works? - Max Mikheyenko
  • In the code: let pubHTML = receivedPubText viewPubHTML.loadHTMLString(pubHTML, baseURL: nil) and in HTML: <img src=\"\/upload\/images\/DSC_34122222222222.JPG\"> - Roman Ekimov

1 answer 1

You have set the wrong baseURL on the loaded page. I see 3 options:

1) (crutch method) Load html as a string, and display with the correct URL.

PS: other links can fly

  let webView: UIWebView! let url: NSURL! let baseURL: NSURL! let session = NSURLSession.sharedSession() let task = session.dataTaskWithURL(url) { (data, _, err) in if let data = data { let str = NSString(data: data, encoding: NSUTF8StringEncoding) dispatch_async(dispatch_get_main_queue()) { webView.loadHTMLString(str, baseURL: baseURL) } } } 

2) Using NSURLProtocol . You can intercept the start of the download, and replace the incorrect NSURL . (very flexible method, I recommend)

3) With the use of JS: go through all the img tags, and replace the url . Here is a small example of use. This operation needs to be performed only after the UIWebView is loaded. This will be the disadvantage of this method, the pictures will start to load with a delay.

  • Thank! I'll try! - Roman Ekimov
  • In my case, only this phrase of yours - "You have entered the wrong baseURL" helped :). In one line everything was fixed: let baseURL = URL(string: "http://info.olimpiada.ru/")viewPubHTML.loadHTMLString(pubHTML, baseURL: baseURL) - Roman Ekimov