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">
Related
When showing a html file in a UIWebView, is it possible to first try to load the images from the DocumentsDirectory, and if it doesn't exist, load from the main bundle?
The reason is I dynamically load the html file from the server and store it locally, and want to do the same with the images. Often times, Images are added and the img tag code below will only load the image from the bundle, and not the documents directory where the downloaded images live.
Thanks in advance
<img src="myImage.jpg"/>
[self.webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
I think that what you need is this code
-(void) loadWebView
{
NSString *termsFilePath = [[NSBundle mainBundle] pathForResource:#"terms_conditions" ofType:#"html"];
NSError *err = nil;
NSString *html = [NSString stringWithContentsOfFile:termsFilePath
encoding:NSUTF8StringEncoding
error:&err];
[webView loadData:[html dataUsingEncoding:NSUTF8StringEncoding] MIMEType:#"text/html" textEncodingName:#"utf-8" baseURL:[NSURL URLWithString:#""]];
}
Hope this helps you
<img src="file:///Users/MyMac/Library/Developer/CoreSimulator/Devices/A6780B96-2F9C-418A-BE5C-5A8136A1B9C4/data/Containers/Data/Application/11245AB0-8736-4931-980C-951604F1CB8B/tmp/myImage.jpg">
As you can see, you need set the image path to src attribute of <image> tag. The path should be file://, I was try without file:// and image didn't show. Hope this helps.
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];
In iOS, I need to load a website in uiwebview and I want to add a local image at the beginning of the webpage.
I added < img src="logo.png"/> after < body > tag but it only showed a null pic(a small white box).
How can I load my local image as the src of the img inside the html? Thanks
Using relative paths or file: paths to refer to images does not work with UIWebView. Instead you have to load the HTML into the view with the correct baseURL:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];
You can then refer to your images like this:
(from uiwebview revisited)
You have to define the extact path of the image source see code below
NSString *imagePath=[[NSBundle mainBundle] pathForResource:#"image" ofType:#"png"];
imagePath=[NSString stringWithFormat:#"file://%#",imagePath];
NSString *html=[NSString stringWithFormat:#"<html><body><img src=\"%#\"/></body></html>",imagePath];
[webView loadHTMLString:html baseURL:nil];
Cheers.
Let me start by saying I know how to load up an html file, but I need to load up index.html?mod=eurt. So the file itself is called index.html, but I need to load it with ?mod=eurt at the end. (the html file is shared between different applications and changing "?mod=X", will tell the file exactly what to do.
The problem is, I cannot figure out how to do this with the way I am loading up the html into the webview (I'm rather new at iOS development, so there may exist an easy way I don't know about). Here's what I have to load a local html file so far:
NSString *htmlFile=[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"
inDirectory:nil];
NSString *htmlString=[NSString stringWithContentsOfFile:htmlFile
encoding:NSUTF8StringEncoding error:nil];
[webView loadHtmlString:htmlString baseURl:nil];
I tried changing the ofType:#"html" to ofType:#"html?mod=eurt", but I didn't really expect that to work, and it didn't.
I've come from the land of Android where I simply did webView.loadUrl("file:///android_asset/index.html?mod=eurt");
There must be an easy way to do this in iOS, right?
You can do something like this
NSString* bundlePath = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSString* strUrl = [[urlToLoad absoluteString] stringByAppendingString:#"?mod=eurt"];
NSURL* urlToLoad = [NSURL URLWithString:strUrl];
[webView loadRequest:[NSURLRequest requestWithURL:urlToLoad]];
in three20's TTCatalog demo, PhotoTest1Controller.m .there's some code
[[[MockPhoto alloc]
initWithURL:#"http://ww4.sinaimg.cn/bmiddle/6e8f23a5jw1dk7pylzs8ij.jpg"
smallURL:#"http://ww4.sinaimg.cn/thumbnail/6e8f23a5jw1dk7pylzs8ij.jpg"
size:CGSizeMake(320, 480)] autorelease],
can i replace these URLs with local path.
i put the pictures into app's bundle.
File URLs (file://) should work just fine. For images in your app bundle you can use -[NSBundle URLForResource:withExtension:]:
NSURL *imageURL = [[NSBundle mainBundle] URLForResource:#"foo" withExtension:#"jpg"];
On iOS < 4.0 you must combine path-based NSBundle methods with +[NSURL fileURLWithPath:]:
NSString *imagePath = [[NSBundle mainBundle] pathForResource:#"foo" ofType:#"jpg"];
NSURL *imageURL = [NSURL fileURLWithPath:imagePath];