Image not displaying from local html on iPhone webview - iphone

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

Related

how to load pdf file in UIWebview center

I am new in ios developement.I am trying to display a pdf in a UIWebview center.i have a pdf in mainbundle.Here is what my PDF looks like now when loaded in a UIWebView: (I put in a brown gradient as the UIWebView's background so you can see what I mean better).
- (void)viewDidLoad
{
UIWebView *theWebView = [[UIWebView alloc] initWithFrame:[self.view bounds]];
[theWebView setContentMode:UIViewContentModeScaleAspectFit];
[theWebView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
[theWebView setScalesPageToFit:YES];
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"pdf"];
NSURL *filePathURL = [NSURL fileURLWithPath:filePath];
[theWebView loadRequest:[NSURLRequest requestWithURL:filePathURL]];
[self.view addSubview:theWebView];
[super viewDidLoad];
}
I would like to centre that so that the bottom margin is the same as the top margin. How would I do this with a PDF (local)?
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];
Not sure whether this is the ideal solution for your question.
First Get the width and height of PDF==>
float width = CGPDFPageGetBoxRect(PageRef, kCGPDFMediaBox).size.width;
float height = CGPDFPageGetBoxRect(PageRef, kCGPDFMediaBox).size.height;
where Pageref is the PDF page reference.
CGSize pdfSize = CGSizeMake(width, height);
//Create one superView to webview
UIView *supView = [[UIView alloc] initWithFrame:[self.view bounds]];
supView.backgroundColor=[UIColor colorWithRed:0.663 green:0.855 blue:0.341 alpha:1]
//Then webview
UIWebView *theWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0, pdfSize.width, pdfSize.height)]];
[theWebView setContentMode:UIViewContentModeScaleAspectFit];
[theWebView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
[theWebView setScalesPageToFit:YES];
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"pdf"];
NSURL *filePathURL = [NSURL fileURLWithPath:filePath];
[theWebView loadRequest:[NSURLRequest requestWithURL:filePathURL]];
[supView addSubview: theWebView];
[self.view addSubview: supView];
EDIT:
Get Pageref(part),
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"pdf"];
NSURL *filePathURL = [NSURL fileURLWithPath:filePath];
CFURLRef pdfURL = (__bridge CFURLRef)[[NSURL alloc] initFileURLWithPath:filePath];
//file ref
CGPDFDocumentRef pdfDocRef = CGPDFDocumentCreateWithURL((CFURLRef) pdfURL);
CGPDFPageRef PageRef = CGPDFDocumentGetPage(pdfDocRef, 1);
float width = CGPDFPageGetBoxRect(page1, kCGPDFMediaBox).size.width;
float height = CGPDFPageGetBoxRect(page1, kCGPDFMediaBox).size.height;
NSLog(#"%f %f",height,width);

Image does not load in UIWebView in 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;

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

How to show a PDF page by page using horizontal swipe in iphone?

I would like to add to my app a pdf viewer which can scroll horizontally. Anyone know how to do it? I'm making several attempts but without success. Thanks. Matteo
Use UIWebViewController in landscape orientation and this piece of code can give you an idea about how to display pdfs:
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake( 0, 0, 320, 480 )];
NSString *path = [[NSBundle mainBundle] pathForResource:#"about" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
webView.scalesPageToFit = YES;
[self.view addSubview:webView];
[webView release];