Loading Content into UIWebView on Iphone
2 answers
Ok, I am assuming you are using loadHTMLString to set the HTML to your UIWebView.
So, to link to images that are stored in your app bundle, you just need to get the correct path to put in your img tag. So enter the path string:
NSString * imgPath = [[NSBundle mainBundle] pathForResource:nameOfImage ofType:@"jpg"];
Format HTML with image tag:
NSString * html = NSString [[[NSString alloc] initWithFormat:@"<img src=\"file://%@\"/>", imgPath] autorelease];
Then install the HTML on the webpage:
[self.webView loadHTMLString:html baseURL:nil];
You just need to add images to your application resources (nameOfImage in the code above) and it will work fine.
+4
source to share
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 simply refer to the image file names:
<img src="your_image.png" />
You don't need to provide a relative path to the images you put in folders. When the application 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/
0
source to share