2

I would like to show Content in the UIWebView, which I have added to the Project. (for example an image)

I know how to set a text in the UIWebView, but I would like to add images or a video which I have added to the project.

Steffen Moritz
  • 7,277
  • 11
  • 36
  • 55
Ploetzeneder
  • 1,281
  • 4
  • 20
  • 34
  • Similar question [answered here](http://stackoverflow.com/questions/1501629/uiwebview-display-locally-stored-website-html-images-javascript/1501908). – Ramin Oct 16 '09 at 20:51

2 Answers2

4

Ok, I am assuming you are using loadHTMLString to set the HTML on your UIWebView.

So to reference images that are stored in your App bundle, you just need to get the right path to put in your img tag. So get the path string:

NSString * imgPath = [[NSBundle mainBundle] pathForResource:nameOfImage ofType:@"jpg"];

Format some HTML with an image tag:

NSString * html = NSString [[[NSString alloc] initWithFormat:@"<img src=\"file://%@\"/>", imgPath] autorelease];

Then set your HTML on the web view:

[self.webView loadHTMLString:html baseURL:nil];

You just need to add the images to your app resources (nameOfImage in the code above) and it will work fine.

RedBlueThing
  • 42,006
  • 17
  • 96
  • 122
  • To get the file URL, use NSURL: http://developer.apple.com/iphone/library/documentation/cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html#//apple_ref/occ/clm/NSURL/fileURLWithPath: – Sixten Otto Oct 16 '09 at 14:47
  • Key phrase : "which i have added to the Project" – RedBlueThing Oct 16 '09 at 23:29
0

Load your HTML into your UIWebView like this:

// assumes you have a UIWebView called 'webView', and an HTML string called 'html'
NSString* imagePath = [[NSBundle mainBundle] resourcePath];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
imagePath = [imagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
[webView loadHTMLString:html baseURL:[NSURL URLWithString:[NSString stringWithFormat:@"file:/%@//", imagePath]]];

Then you can just refer to your images' file names:

<img src="your_image.png" />

You don't need to specify a relative path to images that you've put into folders. When the app is compiled they all go into the root.


Credit: I stole this from here: http://dblog.com.au/iphone-development/loading-local-files-into-uiwebview/

Danyal Aytekin
  • 4,106
  • 3
  • 36
  • 44