Image does not load in UIWebView in iphone - iphone

I have UIWebView i want to load image in that but it does not show image in it i am using following code
pdfView.hidden=NO;
webView.hidden=NO;
pdfView.backgroundColor=[UIColor clearColor];
doneButton = [[UIBarButtonItem alloc]initWithTitle:#" Done " style:UIBarButtonItemStyleBordered target:self action:#selector(doneButtonClick)];
self.navigationItem.leftBarButtonItem =doneButton;
webView.backgroundColor=[UIColor clearColor];
NSLog(#"File Name is %#",fileName);
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:#"png"];
NSURL *url = [NSURL fileURLWithPath:fileName];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView setScalesPageToFit:YES];
[webView loadRequest:request];
is there any way to load this image which is in fileName which comes from sever in this webView thanks

The other way i can go for is to html-page dynamically and load it into UIWebView:
NSString *imageUrlString = #"your url ";
NSString *yourText = #"Some text here";
NSString *htmlString = [NSString stringWithFormat:#"<img src='%#'/><br><b>%#</b>", imageUrlString, yourText];
[myWebView loadHTMLString:htmlString baseURL:nil];

I think you can tighten this up a bit. Instead of getting a string path and creating a file URL from it, just ask the bundle for a URL.
My best guess is your filename already has the file extension in it and you're not actually getting back a valid file path/url. Step through in the debugger and see.
// get the file resource URL
NSURL *url = [[NSBundle mainBundle] URLForResource: filename withExtension: #"png"];
// sanity check - did we get a valid URL?
NSAssert( url != nil && [url checkResourceIsReachableAndReturnError: NULL], #"oops - no resource!");
// load it
[webView loadRequest: [NSURLRequest requestWithURL:url]];

Why can't you just use a UIImageView? This is done by hand but something like;
NSURL *url = [NSURL fileURLWithPath:fileName];
UIImage *tempImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
imageView.image = tempImage;

Related

Can’t Display A Microsoft Word.doc document on UIView Controller

I’m using the code below to display pdfs or filename.doc Microsoft Word documents. It works fine when I use a ofType:#:”pdf”, but my code crashes when I use ofType:#:”doc” or ofType:#:”docx”. The Word document is mostly text and a few url links. Any help appreciated.
NSString *path = [[NSBundle mainBundle] pathForResource:#"C9" ofType:#"doc"];
pdfUrl = [NSURL fileURLWithPath:path];}
[webView loadRequest:[NSURLRequest requestWithURL:pdfUrl]];
As per your code, it looks like you're already using a UIWebView.
Here is some code I've used in the past.
-(void)loadDocument:(NSString*)documentName inView:(UIWebView*)webView {
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
In viewDidLoad (change webView frame to whatever you need)..
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
[self.view addSubview:webView];
[self loadDocument:#"file.doc" inView:webView];
Take a look at using QLPreviewController to display these document types.
For the crash, are you sure the file exists at the specified location? Check it's actually being copied to the bundle and that there is a file at that path / URL before you use it.

Display local pdf file in UIWebView in iphone

I am display local pdf file in UIWebView but it shows exception in path here is my code
NSString*fileName=content_Source;
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView setScalesPageToFit:YES];
[webView loadRequest:request]
You must be using the file name extension while setting the file name. So make sure the extension will not be used in file name if it's setting along with Type (like ofType:#"pdf").
Here's my code:
UIWebView *webView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
webView.backgroundColor = [UIColor redColor];
NSString*fileName= #"About_Downloads";
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:#"pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path isDirectory:NO]];
[webView setScalesPageToFit:YES];
[webView loadRequest:request];
[self.view addSubview:webView];
There's a better way to show PDFs:
UIDocumentInteractionController *dc = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:path isDirectory:NO]];
dc.delegate = self;
[dc presentPreviewAnimated:YES];
Make sure use the delegate along with that code to work it properly.
(UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
return self;
}
first validate that string and the assign to URL like bellow..
NSString *strURLPath = [self trimString: path];
NSURL *url = [NSURL fileURLWithPath:strURLPath];
use my bellow method...
-(NSString*) trimString:(NSString *)theString {
if (theString != [NSNull null])
theString = [theString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
return theString;
}
It is working fine with the given code problem was that in my file Name i was giving also the type like ali.pdf they type pdf so it get exception it worked fine now when i use filename like following
NSString*fileName=#"ali";
Instead of
NSString*fileName=#"ali.pdf";
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize screenSize = rect.size;
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
NSString *path = [[NSBundle mainBundle] pathForResource:#"pdf" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];

Getting application crash during opening a local pdf file on UIWebView

- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSString *path = [[NSBundle mainBundle] pathForResource:#"About" ofType: #"pdf"];
NSLog(#"%#", path);
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[_webView loadRequest:request];
}
just use following code for open PDF in web view
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"About" ofType:#"pdf"];
[self.webVctr loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];
And check it out the PDF is exist or not?
This thinks may helping to you
Happy codeing..
Try this:
in the
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
NSString *path = [[NSBundle mainBundle] pathForResource:#"About" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
[webView release];
It was some other issue not related to the pdf loading over web view. I cross checked and found the actual cause of crash. Anyways thanks guys for the help

Objective-c: Downloading a PDF file

I am trying to eneble a button to download a pdf file from a server.
(Onto the divice)
I'm not being successful with this code (to post this question, i've changed the address)
Would you give me some advice to make it work..?
Thanks in advance.
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://www.mydomain.com/mypdffile.pdf"]];
//Store the Data locally as PDF File
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"<Application_Home>/Documents/"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:#"myPDF.pdf"];
[pdfData writeToFile:filePath atomically:YES];
//Now create Request for the file that was saved in your documents folder
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView setUserInteractionEnabled:YES];
[webView setDelegate:self];
[webView loadRequest:requestObj];
You are not building a correct NSURL object to reach a server. fileURLWithPath: is a method to build URL that points to files in the file system.
Use URLWithString: writing the complete url, that is for example "http://myserver.com/myfile.pdf"

Image not displaying from local html on iPhone webview

I am calling an infoPage.html as below with an image from Resources and referenced as img src="eye.png" in the html, the text displays correctly but I cannot load the image (only a ? appears), any suggestions...
- (void)viewDidLoad {
[super viewDidLoad];
CGRect webRect = CGRectMake(15,40,300,450);
UIWebView *myWebView = [[UIWebView alloc] initWithFrame:webRect];
myWebView.delegate = self;
myWebView.scalesPageToFit = NO;
NSString *htmlPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"infoPage.html"];
NSString *htmlContent = [NSString stringWithContentsOfFile:htmlPath];
[myWebView loadHTMLString:htmlContent baseURL:nil];
//stop webview from scrolling
[[[myWebView subviews] lastObject] setScrollingEnabled:NO];
[myWebView loadHTMLString:htmlContent baseURL:nil];
[self.view addSubview:myWebView];
[myWebView release];
}
You need to use [myWebView loadRequest: [NSURLRequest requestWithURL: [NSURL fileURLWithPath: htmlPath]]], rather than -loadHTML so that it knows where to extract images from. Alternatively, you could pass the path in as the baseURL: argument.
Instead of baseURL:nil, try baseURL:[NSURL URLWithString:[[NSBundle mainBundle] bundlePath]]]