1

Here is loaded HTML code

UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[[NSBundle mainBundle] pathForResource:@"111" ofType:@"jpg"];
NSURLRequest *repuest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.0.5/index.html"]];
[webView loadRequest:repuest];
[self.view addSubview:webView];

Here is the HTML code

<img src="111.jpg" />
<hr />

Do so the image could not be loaded . In this case, How to make HTML loaded into the picture ?

小弟调调
  • 1,315
  • 1
  • 17
  • 33
  • 1
    possible duplicate http://stackoverflow.com/questions/747407/using-html-and-local-images-within-uiwebview. – Baby Groot Apr 24 '13 at 07:30
  • Maybe you need to specify a `baseurl` ? – Xavi López Apr 24 '13 at 07:31
  • No repetition. My HTML is above the server . – 小弟调调 Apr 24 '13 at 07:36
  • As your html is on server. You need to specify whole path of that image in . – Baby Groot Apr 24 '13 at 07:44
  • file://localhost/Users/Michael/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/32057AD3-50AE-47EF-AF78-3BF8638EDDE0/UMSAgentExample.app/111.jpg Replaced this address will not work. – 小弟调调 Apr 24 '13 at 07:54
  • Use this url file:///localhost/Users/Michael/Library/Application%20Support/iPhone%20Simulator/‌​6.1/Applications/32057AD3-50AE-47EF-AF78-3BF8638EDDE0/UMSAgentExample.app/111.jpg – Baby Groot Apr 24 '13 at 08:00
  • See these links, it might help you, https://www.google.co.in/search?q=nsnotification%20tutorial&aq=1&oq=nsnotification%20&aqs=chrome.2.57j0l3j62l2.6813j0&sourceid=chrome&ie=UTF-8#sclient=psy-ab&q=showing+local+image+in+webview+%2B+ios+sdk&oq=showing+local+image+in+webview+%2B+ios+sdk&gs_l=serp.3..33i29i30.905876.918861.0.919029.48.44.3.1.1.1.833.5808.22j19j1j0j1j0j1.44.0...1.0...1c.1.11.psy-ab.Ad0lgjuLfbo&pbx=1&bav=on.2,or.r_cp.r_qf.&fp=e43e71261d25a34d&biw=1020&bih=582 – Baby Groot Apr 24 '13 at 08:01

2 Answers2

0

IF its only one html page without relative links to anything else but the images you can set the base url for the request

that means: all relative urls are considered as relative to this base url


BUT if your html uses css/js/html files on the server AND local images, you would need 2 base urls and this isn't possible.

your only bet is to rewrite the html to only use absolute URLS for anything not local and THEN set the base url.

Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
0

Ok, This is tried and tested code. Should work for you.

NSString *imagePath;
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle bundlePath];
imagePath = [NSBundle pathForResource:@"111" ofType:@"jpg" inDirectory:path];

NSString *fixedURL = [imagePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
fixedURL = [NSString stringWithFormat:@"file://%@",fixedURL];
NSString *commentHtml = [NSString stringWithFormat:@"<img src=\"%@\"/>", fixedURL];
NSString *html3 = [NSString stringWithFormat:@"<html><head/><body><div><b>COMMENTS:</b></div>%@</body></html>", commentHtml];

[webView loadHTMLString:html3 baseURL:nil];
Baby Groot
  • 4,637
  • 39
  • 52
  • 71