How to load large size PDF scroll view in iOS? - iphone

I have pdf of size 53MB when i try to load only one page of PDF in scrollview i got memory warning and then application crash.

Display the PDF in a UIWebView:
NSString *PDFPath = [[NSBundle mainBundle] pathForResource:#"filename" ofType:#"pdf"];
NSURL *URL = [NSURL fileURLWithPath:PDFPath];
NSURLRequest *URLRequest = [NSURLRequest requestWithURL:URL];
[webView loadRequest:URLRequest];
The WebView will load pages into memory as they appear on screen rather than all at once.

This is a duplicate of What is the best way to view large PDFs on iOS?
The accepted answer is to use a UIWebView as indicated above.

You should take a look at the vfr/Reader, which is a PDF Reader Core for iOS.
UIWebView is not very good in handling big files out of the box.

Related

how to scale to fit pages opened from HTML file containing web links

I have an app I am working on in Xcode. Part of the app area loads an html file. The HTML file itself has some links to external websites. They open fine, except they are not scaled to fit on the iPhone.
How would I go about ensuring all web pages opened are scaled to fit? The code from the .m file is below
NSString *fileString = [[NSBundle mainBundle] pathForResource: #"chapter5" ofType: #"html"];
NSURL *newURL = [[NSURL alloc] initFileURLWithPath: fileString];
NSURLRequest *newURLRequest = [[NSURLRequest alloc] initWithURL: newURL];
[webview loadRequest: newURLRequest];
The HTML file "chapter5.html contains links to websites.
I thought I would try entering
webview.scalesPageToFit = YES
in the .m file hoping it would apply to anything opened, but that didn't work.
Any advice would be greatly appreciated.
Thanks
The scalesPageToFit will take effect only when the webView loads the request. Implement the delegate of UIWebView webViewDidFinishLoad: method and try this code.
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGSize defaultSize = webView.frame.size;
CGSize fittingSize = [tableViewCell.webView sizeThatFits:defaultSize];
[webView sizeThatFits:fittingSize];
NSLog(#"Size : %#",NSStringFromCGSize(fittingSize));
}
Please set webview mode as Aspect-Fit
Thanks guys, I set webview mode as Aspect-Fit and set the Scaling to scale pages to fit and this worked, not sure how I missed that.

PDF preview like Mail App

I am looking for a PDF preview feature similar to the one available when a PDF is received via mail in iPad mail app. It has options like Print/Mail and an Open In menu. Do you have any idea is it an apple's custom implementation or part of framework. I couldn't find in QLPreviewController and UIDocInteract framework.
its very simple to implement .. you will just load NSURLRequest with the NSURL that hold the pdf path into UIWebView.
for example :
NSString *pdfPath = [[NSBundle mainBundle] pathForResource:#"iPadHIG" ofType:#"pdf"];
NSURL *pdfUrl = [NSURL fileURLWithPath:pdfPath];
[webView loadRequest:[NSURLRequest requestWithURL:pdfUrl]];
webView.scalesPageToFit = YES;

How do I get rid of white border around youtube video embedded in UIWebView?

In my iPad application I have a UIWebView with a youtube video embedded in it. I have matched the UIWebView size and the size of the embedded video as well as made sure that the aspect ratio was correct. I have set the UIWebView background color to clear color with no luck. How can I center the embedded video inside my UIWebView. I also used content mode: center and it doesn't seem to respect it. See the screenshot below:
http://cl.ly/1N3c0d343r3v1i0J1h0d
NSString *video_ID = #"7Ek1QwGp9Tw?rel=0";
NSString *htmlStr = [NSString stringWithFormat:#"<iframe class=\"youtube-player\" type=\"text/html\" width=\"453\" height=\"255\" src=\"http://www.youtube.com/embed/%#\" frameborder=\"0\"></iframe>",video_ID];
[webView loadHTMLString:htmlStr baseURL:nil];
Any help would be appreciated.
just a wild guess: you need to reset the margins and paddings of your html documents body and the youtube iframe.
edit add something like <html><head><title>.</title><style>body,html,iframe{margin:0;padding:0;}</style></head><body>[YOUR YOUTUBE CODE]</body></html>
Don't use an iframe, just load the video directly in the webview (a webview is basically like a "native" iframe anyway):
NSString *URLString = [NSString stringWithFormat:#"http://www.youtube.com/embed/%#",video_ID];
NSURL *URL = [NSURL URLWithString:URLString];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[webView loadRequest:request];

Iphone Open With in a UIWebView

is it possible to show a PDF in a UIWebView with the option Open in Books and Open With at the top of the WebView? I don't know how to do that.
Thanks
Put a UIDocumentInteractionController on top of the web view, it will allow the user to open the pdf in iBooks.
NSString * path = [[NSBundle mainBundle] pathforResource:#"ResourceName" ofType:#"pdf"] ;
or path can be any http url too.
NSURL * url = [NSURL fileURLWithPath:path];
NSURLRequest * request = [NSURLRequest requestWithURL:url];
UIWebView * webView = ....;
[webView loadRequest:request];
This should show the pdf in webview. But seems you are asking to open the pdf in some other app, which part I didn't understand completely. Can you explain?

Why UIWebView Eating so many Memory?

I'm using a UIWebView to load a pure Text HTML page for my iPad app. The size of the HTMP page is only 40KB. But when I use the instrument to monitor the memory use for loading the UIWebView, I found down it consumes like 20MB memory, if I scroll the web view, the memory is even getting higher. Finally I get a level 1 memory warning.
Could anyone help me with this? How could I reduce the memory for this? (I need to use the HTML to show the text here).
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:#"SPC"];
NSURL *url = [NSURL fileURLWithPath:htmlPath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
htmlPath
url
request
release all of them after this line
[webView loadRequest:request];
then release webview in dealloc and use webview as ivar