Loading bundle images in a UIWebView

September 13th, 2011
#ui

I ran into this problem were I could not get a bundle image to appear in a UIWebView. It turns out that in order to enable this functionality you need to tell the webview where to look for these images, in the below example we tell it to use the main bundle:

NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSURL *bundleBaseURL = [NSURL fileURLWithPath: bundlePath];
    
UIWebView *webview = [[UIWebView alloc] initWithFrame:self.view.frame];
[webview loadHTMLString:self.content baseURL:bundleBaseURL];

And the corresponding HTML line:


<img src="nameOfImage.png" />

What do you think? Let me know by getting in touch on Twitter - @wibosco

William Boles

William has been an iOS developer since 2010, after all these years he just hopes that the String API will stop changing now.