Reputation: 846
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!
Upvotes: 3
Views: 9238
Reputation: 792
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
Upvotes: 0
Reputation: 1
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];
Upvotes: 0
Reputation: 11537
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];
Upvotes: 2