0

I need to display local fles like png,pdf,doc into uiwebview. Can anybody help how to load the local url into webview and display the file in webview..

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
NSString *tempUrlString = [NSString stringWithFormat:@"%@/0_iphone.png",documentsDirectory];

enter image description here

Rajesh Loganathan
  • 10,935
  • 4
  • 76
  • 89

4 Answers4

4

You can use the <img> tag for loading image to UIWebView.

NSString *imagePath  = [[NSBundle mainBundle] pathForResource:@"yourImage" ofType:@"png"];
NSString *htmlString = [NSString stringWithFormat:@"<html><body><img src=\"file://%@\"></body></html>",imagePath];
[myWebView loadHTMLString:htmlString baseURL:nil];

EDIT :

For loading image from document directory, you just need to change the image path in above code, nothing else.

NSArray *docPaths            = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [docPaths objectAtIndex:0];
NSString  *imagePath         = [documentsDirectory stringByAppendingPathComponent:@"/yourImage.png"];
Midhun MP
  • 97,760
  • 30
  • 152
  • 197
4

To load file from your bundle:

  NSString * html = [[NSString alloc] initWithFormat:@"<img src=\"file://%@\"/>", filename];

 [self.webView loadHTMLString:newhtml  baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];

To load from your document directory:

NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask ,YES );
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"index.pdf"];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]]
Mavericks
  • 524
  • 3
  • 5
2

check this...

UIImage *cameraImage = [UIImage imageNamed:@"YourImageName"];

NSData *myData = UIImagePNGRepresentation(cameraImage);

[self.webview loadData:myData MIMEType:@"image/png" textEncodingName:nil baseURL:nil];
Vaisakh
  • 2,899
  • 4
  • 25
  • 41
0

You can simply load any file in UIWebView with passing it as a NSURL

NSURL *url = [NSURL URLWithString:@"your_FIle_name_with_path"]; // Use NSDocumentsDirectory to get local files.
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestURL];
Balram Tiwari
  • 5,587
  • 2
  • 22
  • 41