views:

4767

answers:

2

Hey guys,

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?

+8  A: 

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:

NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];

You can then refer to your images like this:

<img src="myimage.png">

(from http://iphoneincubator.com/blog/windows-views/uiwebview-revisited)

Adam Alexander
That's just what I was looking for, thanks!
Jasarien
A: 

I'm no longer able to do this as of iPhone OS 3.0. :(

More info (StackOverflow.com).

Joe D'Andrea
Should be a comment
Casebash