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
  • 11,129
  • 4
  • 78
  • 90

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
  • 103,496
  • 31
  • 153
  • 200
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,919
  • 4
  • 26
  • 44
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,657
  • 2
  • 23
  • 41