Desperately trying to figue out how to load a local image (saved in local xcode bundle) into the following HTML through a UIWebView. I have spent countless hours following other guides but nothing seems to work, all throw erros. Am trying to add a local image in the following NSString where I have written IMAGEGOESHERE -
{
[super viewDidLoad];
self.title = _restaurant.title;
[[self navigationController] setNavigationBarHidden:NO animated:NO];
self.activityIndicatorView.hidden = NO;
[self.activityIndicatorView startAnimating];
[self loadImageInNewThread];
NSString *webViewContent = [NSString stringWithFormat:#"<html><head><style>* {font-family: Helvetica}</style></head><body><center>%# - %#<br><br><b>Restaurant:</b>%#</b><br>IMAGEGOESHERE<br></center></b></body></html>", _restaurant.openingTime,_restaurant.closingTime, _restaurant.name];
[self.webView loadHTMLString:webViewContent baseURL:nil];
}
I hope you can help as its driving me mad!
You need to reference first the path of your image:
NSString *pathImg = [[NSBundle mainBundle] pathForResource:#"yourimage" ofType:#"png"];
and then specify your path in your webViewContent along with the size of your image:
NSString* webViewContent = [NSString stringWithFormat:
#"<html>"
"<body>"
"<img src=\"file://%#\" width=\"200\" height=\"500\"/>"
"</body></html>", pathImg];
This code should work,
Just replace yourFile with the name of your PDF, and webView with the name of your webView.
Then Replace ofType to the extension of your image.
//PDF View
//Creating a path pointing to a file.pdf
NSString *path = [[NSBundle mainBundle] pathForResource:#"yourFile" ofType:#"pdf"];
//Creating a URL which points towards our path
NSURL *url = [NSURL fileURLWithPath:path];
//Creating a page request which will load our URL (Which points to our path)
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//Telling our webView to load our above request
[webView loadRequest:request];
NSString *pathImg = [[NSBundle mainBundle] pathForResource:#"yourimage" ofType:#"png"];
NSString* webViewContent = [NSString stringWithFormat:
#"<html>"
"<body>"
"<img src=\"file://%#\" width=\"200\" height=\"500\"/>"
[webView loadHTMLString:webViewContent baseURL:nil];
You must add the last line with a base url of nil
Related
I FOUND THE PROBLEM... I found that the value of berria1 have "\n " at the end of the url. where he not get this but that's the problem.
I pass very curious thing and which do not find any explanation.
The fact is that in the application, I'm parsing some news and save these in CoreData, then show a list of news in a UITableView and if I click on one of them brings me to a UIWebView in which position the link with the full story. Now comes the weird ...
If I pass to NSURL the variable which I recovery of CoreData which contains the Web address, do not load, if I pass the same direction to a NSString and this NSString to UIWebView, the UIWebView load this normally. The following is my code.
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.webView.delegate = self;
NSString *berria1 = [[NSString alloc]init];
berria1 = [NSString stringWithFormat:#"%#",self.abisua.noticia];
NSString *berria2 = #"http://www.vitoria-gasteiz.org/we001/was/we001Action.do?idioma=eu&aplicacion=wb021&tabla=contenido&uid=u_47906a5c_14eabe80aeb__7fe9";
NSURL *url = [NSURL URLWithString:berria1];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:urlRequest];
NSLog(#"El contenido de la noticia 1 es %#", berria1);
NSLog(#"El contenido de la noticia 2 es %#", berria2);
}
With the NSLog I see that the values of the variables are the same as you see in the picture below.
The value of the two variables in the NSLog
Image - Debug window - URL is nil
That could be happening?
Thank you.
Its not berria1 which holds the URL its berria2.
NSURL *url = [NSURL URLWithString:berria2];
Here you go give it a try the site is pretty slow so it takes a time to load
NSString *berria2 = #"http://www.vitoria-gasteiz.org/we001/was/we001Action.do?%20%20%20%20idioma=eu&aplicacion=wb021&tabla=contenido&uid=u_47906a5c_14eabe80aeb__7fe9";
NSURL *myUrl = [NSURL URLWithString:berria2];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myUrl];
[self.webView loadRequest:myRequest];
Try this. I didn't see any code how you load the request to webview. But I added the code with that part. Replace it with your webview.
NSString *myurlString = [NSString stringWithFormat:#"%#",self.abisua.noticia];
NSString *cleanedUrl = [myurlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
NSURL *myUrl = [NSURL URLWithString:cleanedUrl];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myUrl];
UIWebView *mywebview = [[UIWebView alloc] init];
[mywebview loadRequest:myRequest];
I finally found and fixed the problem.
The problem was that the variable received whith Core Data and pass to NSString, I do not know how, but the link have added this characters at the end of URL "\n "
Berria1 variable value in debug area was:
berria1 __NSCFString * #"http://www.vitoria-gasteiz.org/we001/was/we001Action.do?idioma=eu&aplicacion=wb021&tabla=contenido&uid=u_47906a5c_14eabe80aeb__7fe9\n " 0x0000000126d9e6c0
Then when I passed this value to NSURL and this found these characters, NSURL value pass to nil.
Then I solved this, passing this NSString to a NSMutableString and erasing these characters before pass to NSURL with this code:
NSString *berria1 = self.abisua.noticia;
NSMutableString * miCadena = [NSMutableString stringWithString: berria1];
[miCadena deleteCharactersInRange: [miCadena rangeOfString: #"\n "]];
Thanks everyone for your help
hi am new to iOS developer,i have seen your code in that your adding second string to url why still understand just try adding 2nd string
NSURL *url = [NSURL URLWithString:berria2];
I have loaded PDF in UIWebView
NSString *thePath = [[NSBundle mainBundle] pathForResource:#"ConferenceMap_2014" ofType:#"pdf"];
if (thePath) {
NSData *pdfData = [NSData dataWithContentsOfFile:thePath];
[webViewPDF loadData:pdfData MIMEType:#"application/pdf" textEncodingName:#"utf-8" baseURL:nil];
}
Error : DiskImageCache: Could not resolve the absolute path of the old directory.
Hi download the class file from here.
then import the "UIView+DocumentView.h"
after that get the path of particular file
NSString *thePath = [[NSBundle mainBundle] pathForResource:#"file1427464777.586564" ofType:#"pdf"];
Next call the class with particular path
[self.view makeDocumentView:thePath];
I am using Webview to load a image file stored in my app Library Directory, first i tried use resourcePath, and bundle path
NSString * html = [[NSString alloc] initWithFormat:#"<img src=\"file://%#\"/>", filename];
[self.webView loadHTMLString:newhtml baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];
the problem is no matter what i set in the baseUrl, i can not load the image correctly , i also tried filePath:
[self.webView loadHTMLString:newhtml baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] filePath]]];
but if i set the absolute path of the image file in the html , all the things is ok, i wonder why?
Have a look at this post: Using HTML and Local Images Within UIWebView
The top answer clearly said that using file: paths to refer to images does not work with UIWebView.
All you need to do is to pass the basepath. Example:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];
Then just need to reference it like this: <img src="myimage.png">
This is what I'm trying to do:
Get a .pdf from external URL
Save it into my local disk
Display it in a WebView
Allow the user to move the .pdf to another app who can read .pdf
Everything from 1 to 3 works fine. But nothing is moved/shared to/with other apps. I can't understand what I'm doing wrong. This is what I'm doing.
How I save the pdf in the Documents folder (viewDidLoad):
// to save the pdf into local file system (tempString is the pdf url)
NSData *pdfData = [[NSData alloc]
initWithContentsOfURL:[NSURL URLWithString:tempString]];
NSString *resourceToPath = [[NSString alloc]
initWithString:[[[[NSBundle mainBundle] resourcePath]
stringByDeletingLastPathComponent]
stringByAppendingPathComponent:#"Documents"]];
NSString *filePAth = [resourceToPath stringByAppendingPathComponent:#"myPDF.pdf"];
[pdfData writeToFile:filePAth atomically:YES];
// to populate the WebView
NSURL *url2 = [NSURL fileURLWithPath:filePAth];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url2];
[my_web_view setUserInteractionEnabled:YES];
//[editoriale_view setDelegate:self];
[my_web_view loadRequest:requestObj];
In my viewDidLoad() function I create a button to allow the user to open a list of apps who can read .pdf files:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self
action:#selector(show_Button)];
And here's my show_Button function:
-(void)show_Button {
NSString *resourceToPath = [[NSString alloc]
initWithString:[[[[NSBundle mainBundle] resourcePath]
stringByDeletingLastPathComponent]
stringByAppendingPathComponent:#"Documents"]];
NSString *filePAth = [resourceToPath
stringByAppendingPathComponent:#"myPDF.pdf"];
NSLog(#"filePath = %#", filePAth);
NSURL *url2 = [NSURL fileURLWithPath:filePAth];
NSLog(#"url2 = %#", url2);
UIDocumentInteractionController *docContr = [UIDocumentInteractionController
interactionControllerWithURL:url2];
[docContr presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}
When I try this on my device everything works fine until I tap on one of the icons in the list (i.e. the iBooks one). Then the app closes (it doesn't crash, it simply closes).
Here's what the console prints for the two logs I put in the show_Button function:
1. filePath = /Users/[MY_USER]/Library/Application Support/iPhone
Simulator/6.1/Applications/[MY_EXAD_APP_ID]/Documents/myPDF.pdf
2. url2 = file://localhost/Users/[MY_USER]/Library/Application%20Support/
iPhone%20Simulator/6.1/Applications/[MY_EXAD_APP_ID]/Documents/myPDF.pdf
Anyone wants to try to make me understand what I'm doing wrong? I'm using Xcode 4.6. I browsed my iPhone app file system with a third-party software and the file "MyPDF.pdf" actually IS in the Documents" folder, and that's clear because the WebView is correctly populated.
Change CGRectZero to self.view.bounds when you display the document controller.
Solved. I had not implemented the UIDocumentenInteractionController delegate in the .h file. Now I have and everything works fine. Thank you to #trojanfoe for the useful hint.
I have an image that I have in my bundle resources that I need to reference as a source file (src="") in my webView stringByEvaluatingJavaScriptFromString. I can reference external images from websites, but I do not know how to write the URL of the local image.
How could this be done?
An example to show how you could do this...
in my viewDidLoad, I am adding some example HTML into a UIWebView as shown below: (This could easily be a loadRequest: call to a remote URL)
NSString *html = #"<html><body><img id='theImage'></img></body></html>";
[self.webView loadHTMLString:html baseURL:nil];
You need to set the view controller as the delegate for the UIWebView, and wait until it has finished loading the HTML. You can then execute a script to amend the image URL:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
// Get the path to the image in the bundle you wish to display, and create a URL from this path.
NSString *imagePath = [[NSString alloc] initWithString:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"MyImage.png"]];
NSURL *imageURL = [NSURL fileURLWithPath:imagePath];
// Create a script to execute in the web view.
NSString *script = [[NSString alloc] initWithFormat:#"document.getElementById('theImage').src = \"%#\";", imageURL];
// Execute the script.
[self.webView stringByEvaluatingJavaScriptFromString:script];
// Tidy up.
[imagePath release];
}