开发者

PDF in webview performance

开发者 https://www.devze.com 2023-01-15 19:26 出处:网络
I am using the following (simple) code to load a PDF from the documents folder in my app into a UIWebView. The performance is very poor. I tried loading the same PDF from the web via Safari and the pe

I am using the following (simple) code to load a PDF from the documents folder in my app into a UIWebView. The performance is very poor. I tried loading the same PDF from the web via Safari and the performance was great. Does anyone have any ideas? (this viewController is being presented as a modalViewController).

- firstView.m

InfoViewController *mcontroller = [[InfoViewController alloc] init];

        NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                             NSUserDomainMask, YES);
        NSStri开发者_StackOverflow社区ng *docsPath = [paths objectAtIndex:0];


NSString *pathToPDF = [NSString stringWithFormat:@"%@/myPDF.PDF",docsPath];

NSURL *targetURL = [NSURL fileURLWithPath:pathToPDF];

mcontroller.urlToFile = targetURL;
[self presentModalViewController:mcontroller animated:YES];

modalViewController.m -

- (void)viewDidLoad {
    [super viewDidLoad];

    NSURLRequest *request = [NSURLRequest requestWithURL:urlToFile];

    [webView loadRequest:request];




}


I ended up using the documentInteractionController for this to display the PDF in Quick Look. There is a great tutorial for this in the 2010 WWDC vids.

No idea why it wasn't working well in webView, but it's smooth as silk in Quick Look.


The first thing I would look at is the PDF its self. Often people try loading huge PDF files and expect them to perform wonderfully. Can you post the PDF for us to see? How large is the file? Remember that the PDF file size is the compressed size, to display images in the PDF they will be uncompressed when stored in RAM. Its not uncommon for a 500KB image on disk to use 20 MB in memory (This applies for all raster graphics, not just PDFs). This is usually the case when PDF performance is poor from what I've seen around here.

I know you said it works great in Safari, however we don't know what all Apple does in Safari's internals vs a UIWebView. Also Safari is usually running in the background, so it is using memory even when your app is running. This reduces mem that you app can use, but when you are running safari as the foremost app, 3rd party apps aren't using much memory, so safari could potentially cache more of the PDF at a time.

0

精彩评论

暂无评论...
验证码 换一张
取 消