I have a UIWebView in my app which I want to use to display an image which will link to another url.
I'm using
<img src="image.jpg" /> to load the image.
The problem is that the image doesn't load (ie. it can't be found) even though it's added as a resource in my project and is copied into the bundle.
I've tried using NSBundle to get the full path of the image and using that and it still doesn't show up in the web view.
Any ideas?
You can add folder (say WEB with sub folders css, img and js and file test.html) to your project by choosing Add Files to "MyProj" and selecting Create folder references. Now the following code will take care about all the referred images, css and javascript
Swift Version of Lithu T.V's answer:
Using relative paths or file: paths to refer to images does not work with UIWebView. Instead you have to load the HTML into the view with the correct baseURL:
You can then refer to your images like this:
(from uiwebview revisited)
These answers did help me -- specifically the file:\\xxxxxxx.xxx, but I had to do a workaround to display the image.
In my case, I have an HTML file on my server which I download to the documents directory. I want to it to display with a local graphic in a UIWebView which I could not get to work. Here's what I did:
So in startup copy the file to documents directory:
Swift version of Adam Alexanders Objective C answer:
I just ran into this problem too. In my case, I was dealing with some images that were not localized and others that were--in multiple languages. A base URL didn't get the images inside localized folders for me. I solved this by doing the following:
Then, use imgHTMLTag in your UIWebView HTML code when you load the contents.
I hope this helps anyone who ran into the same problem.