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.
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.
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.
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/