How to show a local image in the webview? - objective-c

I want to show a local image in the webview , How to do ? somebody has a easy demo ? thanks a lot!!!

The best way to do is set the baseURL to the Bundles Path. Then from there you can call images from your Bundle like so:
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSURL *bundleBaseURL = [NSURL fileURLWithPath: bundlePath];
[webView loadHTMLString:htmlContent baseURL: bundleBaseURL];
The Image in your HTML can then call local images directly.
<img src="yourImageFromTheMainBundle.jpg" />
The same can be used to call CSS files or even JS files because the baseURL looks inside the main Bunble.
If its not in your main bundle, all you have to do is change the baseURL to the path of where the image is located.

NSURL *imageURL = [NSURL fileURLWithPath:localImagePath];
NSURLRequest *request = [NSURLRequest requestWithURL:imageURL];
[[webView mainFrame] loadRequest:request];

Related

Images in my html file in UIWebView not showing

Here is my scenario:
I have a folder called "HTML" in my bundle. This folder has an HTML file and 5 images. The HTML file references all the images at some point using a simple img tag.
<img src="tut_navigation.png" />
Now I have a UIWebview which I used to load the HTML file. All of the contents of the HTML file are rendered correctly except for my images. My images are not showing up for some strange reason. Then I did some research and found related posts saying to load URL like so:
NSString *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"tutorial" ofType:#"html" inDirectory:#"HTML"]];
NSString* htmlString = [NSString stringWithContentsOfFile:url encoding:NSUTF8StringEncoding error:nil];
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[self.webView loadHTMLString:htmlString baseURL:baseURL];
Once I made this change and ran it again, this time only the first image is loading up, the others ones are not. I'm lost as to what the issue is. When I view this HTML file in a web browser, everything is working fine. Any ideas?
The baseURL needs to be the a path to the HTML folder inside the app bundle, not (as you have it) a path to the app bundle itself.
Change this:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[self.webView loadHTMLString:htmlString baseURL:baseURL];
To this:
[self.webView loadHTMLString:htmlString baseURL:url];
You had the right URL for the base URL (url) and then you just threw it away and substituted the URL of the bundle itself, which is wrong.
Even better, forget the base URL and just use a URL instead of a path string for the file itself:
NSString *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"tutorial" ofType:#"html" inDirectory:#"HTML"]];
NSURLRequest* req = [[NSURLRequest alloc] initWithURL:url];
[self.webview loadRequest: req];

UIWebView local Resource main bundle html file

I've tried almost every way of loading an html file into an UIWebView that has images and resouces in the main bundle. The web page always loads but I can never get the images to show up.
NSString *html = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSString *htmldata = [NSString stringWithContentsOfFile:html encoding:NSUTF8StringEncoding error:nil];
NSString *path = [[NSBundle mainBundle] bundlePath];
[webView loadHTMLString:htmldata baseURL:[NSURL URLWithString:path]];
the HTML code has css:
background: url('image.png')
I've also assured that the file exists in the bundle when the app is run using other methods, but still no luck. Anyone see what's wrong with my code???
Instead of loading the HTML directly with loadHTMLString, you can have the web view do the work of loading the HTML from the bundled file. That way it should know where the HTML came from to resolve relative links.
Ex:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"]];
[webview loadRequest:[NSURLRequest requestWithURL:url]];
I haven't tested the above, you may need the inDirectory clause of the pathForResource method. Good luck!
This is an old post, but I think a way to make this work is to change the following line:
[webView loadHTMLString:htmldata baseURL:[NSURL URLWithString:path]];
to:
[webView loadHTMLString:htmldata baseURL:[NSURL fileURLWithPath:path]];
The method in the comment and jimbojw's answer works too, but in my case htmldata is not retrieved from a file, but instead is in memory.

UIWebView load on the server HTML, how to load local images?

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 ?
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.
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];

OS X Web View App - Won't Load Local .html Web Page

I have the following code in an AppDelegate.m file that opens Google when I open my app. I am trying to change it to open a .HTML web page I copied into my Xcode project but it is not working. I am using Xcode 4.5.2.
The code that works to open Google is:
#implementation MDAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com"]];
[self.webView.mainFrame loadRequest:request];
}
I tried changing it to (so that it would load my test.html file I put in the project folder but the app just comes up blank now). What am I missing?
#implementation MDAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"test.html"]];
[self.webView.mainFrame loadRequest:request];
}
As long as your test.html is copied at the root of your project (make sure it's also copied to your final app bundle, huh? - check Build Phases) you can load it up your Web View using this code :
NSString *path = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"html"];
NSURL* fileURL = [NSURL fileURLWithPath:path];
NSURLRequest* request = [NSURLRequest requestWithURL:fileURL];
[self.webView.mainFrame loadRequest:request];
[NSURL URLWithString:#"test.html"] is not creating a valid URL to your file. Try that: (it is from an iOS project, but should apply to OS X apps as well). I'm using loadHTMLString:baseURL: instead of loadRequest:
NSString* fileName = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:fileName encoding:NSUTF8StringEncoding error:NULL];
NSURL *baseURL = [[NSBundle mainBundle] bundleURL];
[self.webView loadHTMLString:htmlString baseURL:baseURL];
[NSURL URLWithString:#"test.html"]
There's your problem. Assuming that your test.html file is in the resource folder of your application bundle, do this instead:
[[NSBundle mainBundle] URLForResource:#"test" withExtension:#"html"];
NSBundle has a few other methods for easily creating URLs in case the one above isn't appropriate (like if the file is in a subdirectory). Take a look at the NSBundle reference page for more info.

UIWebView not loading external images

I'm loading a UIWebView using the loadHTMLString method and depending on the connection speed, the images are sometimes not loaded and I just see a blank rectangle instead of the images. Has anyone seen this issue? Please note that the images are external links.
Maybe you need to set your baseURL. How does your HTML img sections look like? Are you referencing the entire hyperlink including the http://? Make sure the full link is in there and you should have no problem. If you are referencing the images locally in your bundle you use:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"filename" ofType:#"html"];
NSString *HTMLData = [[NSString alloc] initWithContentsOfFile:htmlFile];
[webView loadHTMLString:HTMLData baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
NSURLRequest *req = [NSURLRequest requestWithURL:webUrl];
[self.WebView loadRequest:req];
if images are only external hyperlink