Embed Image in Html and Load in UIWebView - iphone

I know how to load the images in WebView, now I am trying to embed an image in html and load in UIWebView.
The following are the code which I am using,
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectoryPath = [paths objectAtIndex:0];
NSString* folderPath = [documentsDirectoryPath stringByAppendingPathComponent:#"iOSDevices"];
NSArray* filesCount = [[NSFileManager defaultManager ] folderPath error:nil];
NSString* path = [NSString stringWithFormat:#"%#/%#",folderPath,[filesCount objectAtIndex:0]];
NSString *htmlString = [NSString stringWithFormat:#"<html><body><img src=\"%#\" type=\"jpg\" width=\"320\" height=\"480\"></body></html>", path];
[_webView loadHTMLString:htmlString baseURL:nil];
Am I missing something ?

image name not available,path only get image path,not give image in path,add image name in tag

If you are trying to load your image from local, just add this file:// infront of src, like NSString *htmlString = [NSString stringWithFormat:#"<html><body><img src=\"file://%#\" type=\"jpg\" width=\"320\" height=\"480\"></body></html>", path];
and check if you are displaying image "jpg" or "png", and check you are giving correct type in img tag
The html string should be,
<img src="file:///Users/file/...some other paths../Documents/Images/imag.png" type="png" width="320" height="480">

Related

iOS UIWebview Load local device HTML file path [duplicate]

This question already has answers here:
How to load local html file into UIWebView
(18 answers)
Closed 9 years ago.
I am developing one iOS application in which I want to load the HTML file on UIWebView, my html file path is as like below,
iPhone HTML file path:
/var/mobile/Applications/65A8E000-21A6-429E-90A0-6BF03BAB3EA5/SarkBuilderApp.app/SarkBuilerHtmlFile_new.html
I want to load this iPhone device's HTML file path to UIWebView.
How can I do this?
Thank you very much for your quick answer. Let me explain my basic requirement,
I am devleoping one iOS application in which my requirement is to fill some field on the screen and generate a .pdf file.
For that I have taken one pre-defined HTML form, and after that I updated that .html file and saved it via the code below:
NSString *fileName = #"new_file_htmlfile.html";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *html_file_name_location = [documentsDirectory stringByAppendingPathComponent:fileName];
NSError *error;
// Write the file
[data writeToFile:html_file_name_location atomically:YES
encoding:NSUTF8StringEncoding error:&error];
By doing that I have generated the new HTML file and I have read the file path from the code. The iPhone device path is:
/var/mobile/Applications/65A8E000-21A6-429E-90A0-6BF03BAB3EA5/SarkBuilderApp.app/SarkBuilerHtmlFile_new.html
iPhone emulator path is:
/Users/rightwaysolution/Library/Application Support/iPhone Simulator/6.1/Applications/20748991-EE5C-44FE-ACFF-D93542FCF95B/Documents/new_file_htmlfile.html
Now I am trying to load that iPhone device HTML file on my UIWebView so that I can convert it into a .pdf.
Could you please explain to me how I can access that file?
use this code to achieve what you want
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"SarkBuilerHtmlFile_new" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL:nil];
but for this make sure your html file must be within your app folder in xcode
Edits:-
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"txtFile.txt"];
NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
NSString* htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL:nil];
OR
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"txtFile.txt"];
NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
NSString* htmlString = [NSString stringWithContentsOfFile:content encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL:nil];

Replacing image URLs in html string

Hello I have a html string, with tags for every image.
I can extract the url of every image, but what I want is to save those images in the device, and then replace each one's image url with the corresponding path from my sandbox and then display them via UIWebView.
Any ideas how do I do that?
Thank you!
Well if you have the URL for an image, you can save it like this
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL urlWithString:string]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"MyFile"];
[imageData writeToFile:filePath atomically:YES];
To replace the URLs in the HTML string simply use the method
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target
withString:(NSString *)replacement

AirPrint HTML file made locally iPhone

I am currently creating a html file and storing it locally, i give the user the choice of sending the html by HTML or print.
For some reason it is failing to print. if i test -
if([UIPrintInteractionController canPrintData:myData]){
it doesn't get past this point.
Is it possible to print local HTML files if so how to go about it.
Dan
edit code to create html -
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *Daily = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.html",self.title]];
[urlToLoad writeToFile:Daily atomically:YES encoding:NSUTF8StringEncoding error:NULL];
NSString *fileName = [NSString stringWithFormat:#"%#/%#.html", documentsDirectory,self.title];
NSData *myData = [NSData dataWithContentsOfFile:fileName];
[UIPrintInteractionController canPrintData:] is expecting the data to contain properly formatted PDF data, not HTML.
Try doing this instead:
UIPrintFormatter *formatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:html];
[[UIPrintInteractionController sharedPrintController] setPrintFormatter:formatter];

iOS download and save HTML file

I am trying to download a webpage (html) then display the local html that has been download in a UIWebView.
This is what I have tried -
NSString *stringURL = #"url to file";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"index.html"];
[urlData writeToFile:filePath atomically:YES];
}
//Load the request in the UIWebView.
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"]isDirectory:NO]]]; // Do any additional setup after loading the view from its nib.
But this gives a 'SIGABRT' error.
Not too sure what I have done wrong?
Any help would be appreciated. Thanks!
The path passed to the UIWebView is incorrect, like Freerunnering mentioned, try this instead:
// Determile cache file path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:#"%#/%#", [paths objectAtIndex:0],#"index.html"];
// Download and write to file
NSURL *url = [NSURL URLWithString:#"http://www.google.nl"];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
// Load file in UIWebView
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];
Note: Correct error-handling needs to be added.
NSDocumentDirectory will be backed up to iCloud. You might be better off using NSCachesDirectory https://developer.apple.com/library/ios/#qa/qa1719/_index.html
Your app is a folder with a .app extension on the end. On the iPhone once it is installed you can't change this folder, hence why you save it to the documents directory.
In your example code you save the file to Documents/index.html and ask it to load appname.app/index.html.
[NSBundle mainBundle] doesn't give you the documents directory it gives you (i think) the .app folder (though it might be the folder that contains the app, documents, and other folders).
To give you'll want to change this line.
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"]isDirectory:NO]]];
To (providing this is the same method as the rest of the code, else recreate the object 'filePath')
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath isDirectory:NO]]];
Here is the Swift 2.x version:
var paths: [AnyObject] = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
var filePath: String = "\(paths[0])/\("index.html")"
// Download and write to file
var url: NSURL = NSURL(string: "http://www.google.nl")!
var urlData: NSData = NSData.dataWithContentsOfURL(url)
urlData.writeToFile(filePath, atomically: true)
// Load file in UIWebView
web.loadRequest(NSURLRequest(URL: NSURL.fileURLWithPath(filePath)))

UIWebView: Absolute path for images in app documents folder

I have a UIWebView that loads up some html. In it is an img tag, I am saving an image in the app documents folder and I want to display that image within the html, I do so like this
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *tempImgFN = [NSString stringWithFormat:#"tempImg%d.jpg",storyRowID];
NSString *tempImg = [documentsDir stringByAppendingPathComponent:tempImgFN];
[imageBlob writeToFile:tempImg atomically:NO];
tempImg = [tempImg stringByReplacingOccurrencesOfString:#"/" withString:#"//"];
tempImg = [tempImg stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
[string appendString:[NSString stringWithFormat:#"<img style='max-width:120px' src=\"%#\" alt=\"\"/>",tempImg]];
the imageBlob is an NSdata with the data for the image. the image is saved successfully and rest of the html loads up fine but the image is not displayed.
This works fine if I set the baseUrl of the UIwebView to point to the documents folder. But I don't want to do that because I want the baseURL to be [NSBundle mainBundle] resourcePath] to access some javascript files and css
woops that was stupid of me .. just needed to put src=\"file:/%#\"