(Swift) Why webView never display my local images? [duplicate] - html

I have a UIWebView in my app which I want to use to display an image which will link to another url.
I'm using
<img src="image.jpg" /> to load the image.
The problem is that the image doesn't load (ie. it can't be found) even though it's added as a resource in my project and is copied into the bundle.
I've tried using NSBundle to get the full path of the image and using that and it still doesn't show up in the web view.
Any ideas?

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:
<img src="myimage.png">
(from uiwebview revisited)

Use this:
[webView loadHTMLString:htmlString baseURL:[[NSBundle mainBundle] bundleURL]];

I just ran into this problem too. In my case, I was dealing with some images that were not localized and others that were--in multiple languages. A base URL didn't get the images inside localized folders for me. I solved this by doing the following:
// make sure you have the image name and extension (for demo purposes, I'm using "myImage" and "png" for the file "myImage.png", which may or may not be localized)
NSString *imageFileName = #"myImage";
NSString *imageFileExtension = #"png";
// load the path of the image in the main bundle (this gets the full local path to the image you need, including if it is localized and if you have a #2x version)
NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageFileName ofType:imageFileExtension];
// generate the html tag for the image (don't forget to use file:// for local paths)
NSString *imgHTMLTag = [NSString stringWithFormat:#"<img src=\"file://%#\" />", imagePath];
Then, use imgHTMLTag in your UIWebView HTML code when you load the contents.
I hope this helps anyone who ran into the same problem.

try use base64 image string.
NSData* data = UIImageJPEGRepresentation(image, 1.0f);
NSString *strEncoded = [data base64Encoding];
<img src='data:image/png;base64,%# '/>,strEncoded

I had a simmilar problem, but all the suggestions didn't help.
However, the problem was the *.png itself. It had no alpha channel. Somehow Xcode ignores all png files without alpha channel during the deploy process.

In Swift 3:
webView.loadHTMLString("<img src=\"myImg.jpg\">", baseURL: Bundle.main.bundleURL)
This worked for me even when the image was inside of a folder without any modifications.

You can add folder (say WEB with sub folders css, img and js and file test.html) to your project by choosing Add Files to "MyProj" and selecting Create folder references. Now the following code will take care about all the referred images, css and javascript
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"WEB/test.html" ofType:nil];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];

After having read a couple of chapters in the iOS 6 Programming Cookbok and started to learn objective-c and iOS programming, I would just like to add, that if one is going to load resources from a custom bundle and use that in a web view, it can be accomplished like this:
NSString *resourcesBundlePath = [[NSBundle mainBundle] pathForResource:#"Resources" ofType:#"bundle"];
NSBundle *resourcesBundle = [NSBundle bundleWithPath:resourcesBundlePath];
[self.outletWebView loadHTMLString:[html description] baseURL:[resourcesBundle bundleURL]];
Then, in your html you can refer to a resource using the "custom" bundle as your base path:
body {
background-image:url('img/myBg.png');
}

Swift Version of Lithu T.V's answer:
webView.loadHTMLString(htmlString, baseURL: NSBundle.mainBundle().bundleURL)

Swift version of Adam Alexanders Objective C answer:
let logoImageURL = NSURL(fileURLWithPath: "\(Bundle.main.bundlePath)/PDF_HeaderImage.png")

If you use relative links to images then the images won't display as all folder structures are not preserved after the iOS app is compiled. What you can do is convert your local web folder into a bundle instead by adding the '.bundle' filename extension.
So if you local website is contained in a folder "www", this should be renamed to "www.bundle". This allows the image folders and directory structure to be preserved. Then load the 'index.html' file into the WebView as an HTML string with 'baseURL' (set to www.bundle path) to enable loading relative image links.
NSString *mainBundlePath = [[NSBundle mainBundle] resourcePath];
NSString *wwwBundlePath = [mainBundlePath stringByAppendingPathComponent:#"www.bundle"];
NSBundle *wwwBundle = [NSBundle bundleWithPath:wwwBundlePath];
if (wwwBundle != nil) {
NSURL *baseURL = [NSURL fileURLWithPath:[wwwBundle bundlePath]];
NSError *error = nil;
NSString *page = [[NSBundle mainBundle] pathForResource:#"index.html" ofType:nil];
NSString *pageSource = [NSString stringWithContentsOfFile:page encoding:NSUTF8StringEncoding error:&error];
[self.webView loadHTMLString:pageSource baseURL:baseURL];
}

These answers did help me -- specifically the file:\\xxxxxxx.xxx, but I had to do a workaround to display the image.
In my case, I have an HTML file on my server which I download to the documents directory. I want to it to display with a local graphic in a UIWebView which I could not get to work. Here's what I did:
Copy the file from the NSBundle to the local documents directory
Reference the file in my HTML document as "file:\\filename.png"
So in startup copy the file to documents directory:
-(BOOL)copyBundleFilesToDocumentsDirectoryForFileName:(NSString *)fileNameToCopy OverwriteExisting:(BOOL)overwrite {
//GET DOCUMENTS DIR
//Search for standard documents using NSSearchPathForDirectoriesInDomains
//First Param = Searching the documents directory
//Second Param = Searching the Users directory and not the System
//Expand any tildes and identify home directories.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
//COPY FILE FROM NSBUNDLE File to Local Documents Dir
NSString *writableFilePath = [documentsDir stringByAppendingPathComponent:fileNameToCopy];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *fileError;
DDLogVerbose(#"File Copy From Bundle to Documents Dir would go to this path: %#", writableFilePath);
if ([fileManager fileExistsAtPath:writableFilePath]) {
DDLogVerbose(#"File %# already exists in Documents Dir", fileNameToCopy);
if (overwrite) {
[fileManager removeItemAtPath:writableFilePath error:nil];
DDLogVerbose(#"OLD File %# was Deleted from Documents Dir Successfully", fileNameToCopy);
} else {
return (NO);
}
}
NSArray *fileNameParts = [fileNameToCopy componentsSeparatedByString:#"."];
NSString *bundlePath = [[NSBundle mainBundle]pathForResource:[fileNameParts objectAtIndex:0] ofType:[fileNameParts objectAtIndex:1]];
BOOL success = [fileManager copyItemAtPath:bundlePath toPath:writableFilePath error:&fileError];
if (success) {
DDLogVerbose(#"Copied %# from Bundle to Documents Dir Successfully", fileNameToCopy);
} else {
DDLogError(#"File %# could NOT be copied from bundle to Documents Dir due to error %#!!", fileNameToCopy, fileError);
}
return (success);
}

My complex solution (or tutorial) for rss-feed (get in RSSItems) works only on device:
#define CACHE_DIR [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]
for (RSSItem *item in _dataSource) {
url = [NSURL URLWithString:[item link]];
request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"GET"];
[NSURLConnection sendAsynchronousRequest:request
queue:queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
#autoreleasepool {
if (!error) {
NSString *html = [[NSString alloc] initWithData:data
encoding:NSWindowsCP1251StringEncoding];
{
NSError *error = nil;
HTMLParser *parser = [[HTMLParser alloc] initWithString:html error:&error];
if (error) {
NSLog(#"Error: %#", error);
return;
}
HTMLNode *bodyNode = [parser body];
NSArray *spanNodes = [bodyNode findChildTags:#"div"];
for (HTMLNode *spanNode in spanNodes) {
if ([[spanNode getAttributeNamed:#"class"] isEqualToString:#"page"]) {
NSString *absStr = [[response URL] absoluteString];
for (RSSItem *anItem in _dataSource)
if ([absStr isEqualToString:[anItem link]]){
NSArray *spanNodes = [bodyNode findChildTags:#"img"];
for (HTMLNode *spanNode in spanNodes){
NSString *imgUrl = [spanNode getAttributeNamed:#"src"];
if (imgUrl){
[anItem setImage:imgUrl];
break;
}
}
[anItem setHtml:[spanNode rawContents]];
[self subProcessRSSItem:anItem];
}
}
}
[parser release];
}
if (error) {
NSLog(#"Error: %#", error);
return;
}
[[NSNotificationCenter defaultCenter] postNotificationName:notification_updateDatasource
object:self
userInfo:nil];
}else
NSLog(#"Error",[error userInfo]);
}
}];
and
- (void)subProcessRSSItem:(RSSItem*)item{
NSString *html = [item html];
if (html) {
html = [html stringByReplacingOccurrencesOfString:#"<div class=\"clear\"></div>"
withString:#""];
html = [html stringByReplacingOccurrencesOfString:#"<p class=\"link\">"
withString:#""];
html = [html stringByReplacingOccurrencesOfString:#"<div class=\"page\">"
withString:#""];
html = [html stringByReplacingOccurrencesOfString:#"</div>"
withString:#""];
NSArray *array1 = [html componentsSeparatedByString:#"<a"];
if ([array1 count]==2) {
NSArray *array2 = [html componentsSeparatedByString:#"a>"];
html = [[array1 objectAtIndex:0] stringByAppendingString:[array2 objectAtIndex:1]];
}
NSURL *url;
NSString *fileName;
NSString *filePath;
BOOL success;
if ([item image]) {
url = [NSURL URLWithString:
[hostString stringByAppendingString:[item image]]];
NSData *imageData = [NSData dataWithContentsOfURL:url];
fileName = [[[url relativePath] componentsSeparatedByString:#"/"] lastObject];
filePath = [NSString stringWithFormat:#"%#/%#",
CACHE_DIR,
fileName];
//save image locally
success = [[NSFileManager defaultManager] createFileAtPath:filePath
contents:imageData
attributes:nil];
//replace links
html = [html stringByReplacingOccurrencesOfString:[item image]
withString:filePath];
[item setImage:fileName];
//Передадим обновление интерфейса, снабдив индексом обновляемой ячейки
[[NSNotificationCenter defaultCenter] postNotificationName:notification_updateRow
object:self
userInfo:[NSDictionary dictionaryWithObject:#([_dataSource indexOfObject:item])
forKey:#"row"]];
}
//finalize html
html = [NSString stringWithFormat:#"<html><body>%#</body></html>",html];
fileName = [[[item link] componentsSeparatedByString:#"/"] lastObject];
filePath = [NSString stringWithFormat:#"%#/%#",
CACHE_DIR,
fileName];
success = [[NSFileManager defaultManager] createFileAtPath:filePath
contents:[html dataUsingEncoding:NSUTF8StringEncoding]
attributes:nil];
[item setHtml:
(success)?filePath:nil];//for direct download in other case
}
}
on View controller
- (void)viewDidAppear:(BOOL)animated{
RSSItem *item = [[DataSingleton sharedSingleton] selectedRSSItem];
NSString* htmlString = [NSString stringWithContentsOfFile:[item html]
encoding:NSUTF8StringEncoding error:nil];
NSURL *baseURL = [NSURL URLWithString:CACHE_DIR];
[_webView loadHTMLString:htmlString
baseURL:baseURL];
}
rss item class
#import <Foundation/Foundation.h>
#interface RSSItem : NSObject
#property(nonatomic,retain) NSString *title;
#property(nonatomic,retain) NSString *link;
#property(nonatomic,retain) NSString *guid;
#property(nonatomic,retain) NSString *category;
#property(nonatomic,retain) NSString *description;
#property(nonatomic,retain) NSString *pubDate;
#property(nonatomic,retain) NSString *html;
#property(nonatomic,retain) NSString *image;
#end
part of any html with image
<html><body>
<h2>blah-blahTC One Tab 7</h2>
<p>blah-blah НТС One.</p>
<p><img width="600" height="412" alt="" src="/Users/wins/Library/Application Support/iPhone Simulator/5.0/Applications/2EAD8889-6482-48D4-80A7-9CCFD567123B/Library/Caches/htc-one-tab-7-concept-1(1).jpg"><br><br>
blah-blah (Hasan Kaymak) blah-blah HTC One Tab 7, blah-blah HTC One. <br><br>
blah-blah
microSD.<br><br>
blah-blah Wi-Fi to 4G LTE.</p>
</p>
</body></html>
image saved for name htc-one-tab-7-concept-1(1).jpg

Related

Load html file from a framework is this possible?

Hi I want to load html and .js file from my project. These are the below steps i am following to load html file through framework but i am not able to hit html files.
Steps followed.
Created a framework which has webview as subview in it.
Created Resource.bundle and added all my html,.js files and images in the bundle.
When i add this framework into my project and try to access the image from the bundle i am not able to.
when i try to hit the html file i am not able to laod the html file in my webview.
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:#"Resource" ofType:#"bundle"];
NSString *htmlFile = [[NSBundle bundleWithPath:bundlePath] pathForResource:#"simple" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
NSURL *url=[[NSBundle mainBundle] bundleURL];
[webview loadHTMLString:htmlString baseURL:url];
[self addSubview:webview];
Can anyone tell me why this wont work and is this the right approach to follow?
I create a bundle with index.html that call a js with a simple alert, with this code i'm able to se the alert
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:#"BundleName" ofType:#"bundle"];
NSString *htmlFile = [[NSBundle bundleWithPath:bundlePath] pathForResource:#"index" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[_web loadHTMLString:htmlString baseURL:[[NSBundle mainBundle] bundleURL]];
the html work in local? the webview is initialized?

How to show an image saved on Documents Directory inside local HTML? iOS

I've this app which needs to store images shown in a webview locally, and show those stored images again inside the same webview.
This what I've gotten so far...
// After download button clicked... Store image from URL locally
NSURL *url = [NSURL URLWithString:myURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"filename.png"];
[urlData writeToFile:filePath atomically:YES];
}
// Get image path from Documents Directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"filename.png"];
// Display local html page
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"revistasPhotoswipe" ofType:#"html" inDirectory:#"www"]];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webView loadRequest:requestObj];
And in html file there's just a tag to show the image
<html>
<head></head>
<body><img src='filename.png' /></body>
</html>
Which is the right way to communicate the Documents Dir, with App Bundle and the HTML files to make this work? Another approach?
You can't use <body><img src='filename.png' /></body> because it references an object on a localserver that doesn't exist. You have a couple of options:
1. Javascript
A milliseconds after the page has loaded you should tell your webview to evaluate a javascript function.document["pictureIdFromProgam"].src = searchPic.src; as well as declaring your image element with <img src='throw.imgNotSet' id='pictureIdFromProgam'/>
2. String Manipulatoin
Another (less desirable, but quicker) option is to have your HTML file coded into an NSString and inserting the img link by using either replacing or %#s
Insertion:
[NSString stringWithFormat:#"<html><blah blah blah/><img src=%#/>",imagePathAsString];
and then you will be able to tell your webview to load the nsstring which contains the updated web content.

load a local css file into a webview

I built a simple ios app that loads a html downloaded from a server into a webview.
[self.webView loadHTMLString:notif.html baseURL:nil];
This works perfect. The problem is that i nedd to apply a local css file to the webview and I dont know how to do it.
I tried doing it this way, but the styles are not being applied.
NSString *HTML_HEADER=#"<HTML><HEAD><link rel='stylesheet' href='style.css' type='text/css'></HEAD><BODY>";
NSString *HTML_FOOTER=#"</BODY></HTML>";
NSString *htmlString = [NSString stringWithFormat:#"%#%#%#",HTML_HEADER,notif.html,HTML_FOOTER];
NSLog(#"htmlString %#", htmlString);
[self.webView loadHTMLString:htmlString baseURL:nil];
Any idea?
Thanks
UPDATE:
I tried to do the following:
NSString *HTML_HEADER=#"<HTML><HEAD><link rel='stylesheet' href='#FILE1#' type='text/css'></HEAD><BODY>";
NSString *HTML_FOOTER=#"</BODY></HTML>";
NSString *cssFilePath = [[NSBundle mainBundle] pathForResource:#"style" ofType:#"css"];
NSString *html_header_with_files = [HTML_HEADER stringByReplacingOccurrencesOfString:#"#FILE1#" withString:cssFilePath];
NSString *htmlString = [NSString stringWithFormat:#"%#%#%#",html_header_with_files,notif.html,HTML_FOOTER];
NSLog(#"htmlString %#", htmlString);
[self.webView loadHTMLString:htmlString baseURL:nil];
And in Build Phases i have added the style.css
UPDATE 2:
I also check if the style.css exists with
NSString* filePath = [[NSBundle mainBundle] pathForResource:#"style" ofType:#"css"];
NSLog(#"\n\nthe string %#",filePath);
The reason is CSS and JS files not available at runtime for render in HTML.
You need to take care of some points:
All CSS/JS/HTML files must be added to Project Properties >> Build Phases >> Copy Bundle Resources.
Check that all files are added if not then manually add those files.
All files are should be placed in the same folder, so that reference file path will be available at run time.
Instead of using directly file names, I will prefer you to use marker. Use symbols as a marker to replace with actual path of CSS and JS files.
For example:
NSString *HTML_HEADER=#"<HTML><HEAD><link rel='stylesheet' href='#FILE1#' type='text/css'></HEAD><BODY>";
NSString *HTML_FOOTER=#"</BODY></HTML>";
NSString *cssFilePath = [[NSBundle mainBundle] pathForResource:#"style" ofType:#"css"];
NSString *html_header_with_files = [HTML_HEADER stringByReplacingOccurrencesOfString:#"#FILE1#" withString:cssFilePath];
NSString *htmlString = [NSString stringWithFormat:#"%#%#%#",html_header_with_files,notif.html,HTML_FOOTER];
NSLog(#"htmlString %#", htmlString);
[self.webView loadHTMLString:htmlString baseURL:nil];
In above example I have replace style.css string from html part and add marker #FILE1#.
This marker is relative url to file style.css and it will be replace before html load in webView.
So whenever you would like to load html to webView, load all resource files from their relatives urls.
Here HTML_HEADER will be replace with actual path for all resources files.
You can import files (as CSS, JS ...) even without putting them into "Copy bundle resources", so you can just download CSS and use it during run of your app.
Trick is in setting "baseURL" parameter, which becomes root directory for your UIWebView.
// HTML file
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:htmlFileName ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
// root directory
NSURL *rootDir = [[NSBundle mainBundle] bundleURL];
[self.webView loadHTMLString:htmlString baseURL:rootDir];
Then, when you say
<link rel="stylesheet" type="text/css" href="style.css">
in your HTML file, app will look for style.css in rootDir directory (in this case, [[NSBundle mainBundle] bundleURL] )
I finally find the solution.
NSString *HTML_HEADER=#"<HTML><HEAD><link rel='stylesheet' href='#FILE1#' type='text/css'></HEAD><BODY>";
NSString *HTML_FOOTER=#"</BODY></HTML>";
NSString *cssFilePath = [[NSBundle mainBundle] pathForResource:#"style" ofType:#"css"];
NSString *html_header_with_files = [HTML_HEADER stringByReplacingOccurrencesOfString:#"#FILE1#" withString:cssFilePath];
NSString *htmlString = [NSString stringWithFormat:#"%#%#%#",html_header_with_files,notif.html,HTML_FOOTER];
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
NSLog(#"htmlString %#", htmlString);
[self.webView loadHTMLString:htmlString baseURL:baseURL];
Its necessary to specify the bundlePath in loadHTMLString:
The solution is a combination of the two answers given by #Kampai and #itzprintz

Loading local html from an array in xcode

I am attempting to load a locally hosted html file into a webview from an array. it looks something like this
_siteAddresses = [[NSArray alloc]
initWithObjects:#"file://localhost/var/mobile/Application/${APP_ID}/First Pentecostal Seminary.app/First_Pentecostal_Seminary/Main.html",...
with the corresponding code being
NSString *urlString = [_siteAddresses
objectAtIndex:indexPath.row];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
self.detailViewController.webView.scalesPageToFit = YES;
[self.detailViewController.webView loadRequest:request];
What am I doing wrong...is it my coding or perhaps the html coding (I believe there may be some java in there)
Two thoughts:
That example file URL doesn't look right. How did you construct that? If it was something from your bundle on your device, I'd expect something more like:
file:///var/mobile/Applications/9E670C3C-C8B1-4B09-AE66-B43F7DB29F4D/First Pentecostal Seminary.app/...
Obviously, you have to programmatically determine this URL by using NSBundle instance method URLForResource or by using the bundle's bundleURL and then adding the appropriate path components.
You should (a) specify the view controller to be the delegate for your web view; and then (b) implement webView:didFailLoadWithError: and look at the error there, and it will inform you what the error was, if any.
For example, I have a file, test.html sitting in my bundle, which I can load into a web view like so:
NSURL *bundleURL = [[NSBundle mainBundle] bundleURL];
NSURL *url = [bundleURL URLByAppendingPathComponent:#"test.html"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
I have set up my view controller as the delegate for the web view and have the following didFailLoadWithError:
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(#"%s: %#", __FUNCTION__, error);
}
So, when I tried to load a request with an invalid URL this time (test2.html, which I do not have in my bundle), I get the following error:
2014-02-26 23:35:43.593 MyApp[3531:70b] -[ViewController webView:didFailLoadWithError:]: Error Domain=NSURLErrorDomain Code=-1100 "The requested URL was not found on this server." UserInfo=0x8c32f40 {NSErrorFailingURLStringKey=file:///Users/user/Library/Application%20Support/iPhone%20Simulator/7.0.3/Applications/FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF/MyApp.app/test2.html, NSErrorFailingURLKey=file:///Users/user/Library/Application%20Support/iPhone%20Simulator/7.0.3/Applications/FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF/MyApp.app/test2.html, NSLocalizedDescription=The requested URL was not found on this server., NSUnderlyingError=0x10424c90 "The requested URL was not found on this server."}
Try this code
UIWebView *documentsWebView=[[UIWebView alloc]init];
documentsWebView.delegate=self;
documentsWebView.frame=CGRectMake(0, 0, 1024, 616);
documentsWebView.backgroundColor=[UIColor clearColor];
[self.view addSubview:documentsWebView];
NSString* htmlString = [NSString stringWithContentsOfFile:[_siteAddresses
objectAtIndex:indexPath.row] encoding:NSUTF8StringEncoding error:nil];
[documentsWebView loadHTMLString:htmlString baseURL:nil];

How to read image file at documents directory from ios webview

I am downloading image file and write to documents directory like below:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *objURL = #"http://test.test.com/file/image.png";
NSURL *objurl = [NSURL URLWithString:objURL];
NSData *imgData = [NSData dataWithContentsOfURL:objurl];
NSString *imgFile = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"image.png"];
[imgData writeToFile:imgFile atomically:YES];
In my webView, I loaded a html file from my bundle resource to use in my webView, but how do I have the tag in html to read the image.png in documents directory?
<html>
<body>
<img src="../what/is/the/path/to/use/image.png" />
</body>
</html>
You could give the URL for this image using an absolute path with a file:// scheme:
NSString *documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES)[0];
NSString *filePath = [NSString stringWithFormat:#"file://%#/image.png", documentsDirectory];
You could then run some javascript to update the src tag to update it to the new path:
NSString *javascript = [NSString stringWithFormat:#"var imageElement = document.getElementById('localFile'); imageElement.setAttribute('src', '%#');", filePath];
[self.webView stringByEvaluatingJavaScriptFromString:javascript];
In your HTML the path would look like something like:
<html>
<body>
<img id="localFile" src="file:///var/mobile/Applications/3D7D43E8-FA5E-4B19-B74C-669F7D1F3093/Documents/image.png" />
</body>
</html>
Instead of evaluating JavaScript, I just write down a placeholder in my html content, and replace it directly w/ the content needed:
NSString *htmlFilePath = [[NSBundle mainBundle] pathForResource:#"sample" ofType:#"html"];
NSString *htmlString = [NSString stringWithContentsOfFile:htmlFilePath encoding:NSUTF8StringEncoding error:&error];
if (error) {
// handle error
return;
}
NSString *imageFilePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:#"sample_image.png"];
NSURL *imageFileURL = [NSURL fileURLWithPath:imageFilePath];
NSString *sampleImageReplacement =
[NSString stringWithFormat:#"<img src=\"%#\" />", [imageFileURL absoluteString]];
// Replace the placeholder w/ real content,
// and of course, we can also replace it w/ empty string
// if the image cannot be found.
htmlString = [htmlString stringByReplacingOccurrencesOfString:#"%sample_image%"
withString:sampleImageReplacement];
...
[webView loadHTMLString:htmlString baseURL:nil];
You can load the images from the app bundle but you can not load the images from the documents directory directly.
But there is a work around.
Use CocoaHTTPServer to create a InAppHttpServer and you can access the images in documents directory using http URL in web view.
or else
Convert the image into base64 and load the image using evaluatejavascript of webview
document.getElementById('img').setAttribute( 'src', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==' );
You can not bind image source from beginning. You will have to do it via JavaScript.
You need to pass the path of the image to UIWebView by evaluating JS. Here you can pass the path of your image and JS will load the image into respective HTML tag.