UIWebView: Absolute path for images in app documents folder - iphone

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:/%#\"

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];

Embed Image in Html and Load in UIWebView

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">

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];

Adding images into the iPhone application

I have an application where the user should be able to take pictures using camera and save them within the application. By default all the images taken are saved to the photo album.But I want to bring them into my project just like the way we add required images for the project into the Resources folder in Xcode. At the same time I want them to converted into PNG format.
After you have taken image from Camera you can save this image to your application's document directory as,
UIImage *image = imageFromCamera;
NSData *imageData = UIImagePNGRepresentation(image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:#"MyImage.png"];
[imageData writeToFile:fullPathToFile atomically:YES];
Its tested code and working absolutely fine.
here is a great tutorial that you should check out http://iosdevelopertips.com/camera/camera-application-to-take-pictures-and-save-images-to-photo-album.html
And here is the documentation from Apple for the UIImagePickerController that you should use https://developer.apple.com/documentation/uikit/uiimagepickercontroller
NSArray *UsrDocPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *DocsDir = [UsrDocPath objectAtIndex:0];
NSString *path = [DocsDir stringByAppendingPathComponent:#"image.jpg"];
//Check if image exists on documents path, if exists removed it, but it depends on your logic, you can add more images in documents path by changing its name...
BOOL success = [FileManager fileExistsAtPath:zipPath];
if(success){
[FileManager removeItemAtPath:path error:&error];
}
[UIImageJPEGRepresentation(image,1.0) writeToFile:path atomically:YES];