PDFKit - PDFView using pageViewController - page rendering slow when swiping to next page
Asked Answered
L

3

12

I have a PDFView that's set to use a pageViewController:

    let pdfView = PDFView()
    let pdfDoc = PDFDocument(url: Bundle.main.url(forResource: "test", withExtension: "pdf")!)
    pdfView.document = pdfDoc
    pdfView.autoScales = true
    pdfView.displayDirection = .horizontal
    pdfView.usePageViewController(true, withViewOptions: [:])

Whenever I swipe to get to the next page, said next page is blurry for half a second before it's rendered sharply. That is quite annoying. Can I somehow preload the pages? I haven't found any attributes/methods in the documentation.

(Interestingly, I have the exact same problem in Preview on MacOS when it's in fullscreen mode [since forever, on every mac I own])

It's not reliant on pdf (file) size. The problem occurs with every pdf I tried.

Thanks for any help

Longish answered 17/3, 2018 at 11:59 Comment(10)
Would you please try with this code: if let documentURL = Bundle.main.url(forResource: "download", withExtension: "pdf"), let document = PDFDocument(url: documentURL), let page = document.page(at: 0) { pdfView?.document = document pdfView?.autoScales = true pdfView?.backgroundColor = UIColor.lightGray pdfView?.displayDirection = .horizontal pdfView?.displayMode = .singlePageContinuous pdfView?.usePageViewController(true, withViewOptions: [:]) }Alfred
Still the same problem. This also just adds pdfView?.displayMode = .singlePageContinuous and why do you need let page = document.page(at: 0) when its not being used?Longish
This code works perfectly for me. I have some other code regarding the page, I just gave you the pdf loading part code. Did you test your code on a device?Alfred
ah ok. Yes, I tested it on Ipad Pro 2, iPhone 7 and the Simulator. It's the same on every device. It's very strangeLongish
I tried to capture the moment it's rendering using an example pdf: imgur.com/a/wtBxRLongish
could you share your pageViewController code? I think the problem is not the pdf rendering but the page creation for the pageVCSirkin
I don't have code for a pageViewController. I just set pdfView.usePageViewController(true, withViewOptions: [:]) without doing anything elseLongish
That’s the difference to commercial PDF SDKs like pspdfkit.com - we go the extra mile and pre-cache pages. (We work on that since 2011.)Magaretmagas
Yeah, but I don't want to spend money on it (and I can't). I simply created my own pdf reader that's working perfectly, so I don't really need it anyway. It's still strange that PDFKit has this problem, though.Longish
Anyone else experience flickering while swiping over the PDFThumbnailView on a pdf with 30> pages?Betimes
R
1

I think this is due to the high resolution of your PDF and the way PDFView renders PDF. Do you have more information on your PDF?

Could you try with a PDF with less heavy images? It should render fine. If so, it's not your code which is at fault, but the resources needed to display and render it to the view.

UPDATE

You can try using the PDFView without PageViewController and see you it behaves. You could do this:

pdfView = PDFView(frame: view.frame)
pdfView.backgroundColor = UIColor.white

var documentName: String = "test"

if let documentURL = Bundle.main.url(forResource: documentName, withExtension: "pdf") {
    if let document = PDFDocument(url: documentURL) {
        pdfView.autoScales = true
        pdfView.displayDirection = .horizontal
        pdfView.displayMode = .singlePageContinuous
        pdfView.document = document
    }
}

self.view.addSubview(pdfView)

Does it behave differently? I've notice that the option usePageViewController loads the document faster for big PDF documents. Something to take into consideration when implementing.

I hope this helps

Reunionist answered 27/3, 2018 at 7:55 Comment(2)
No, it's not the resolution. As I said, I tried multiple different PDF. I tried huge high-resolution image PDFs and text-only small PDF. The result was the same (The screenshot in the comments is from a high-resolution one, only because it's easier to see there). And the code above is literally all I have written (besides setting AutoLayout constraints and adding it as subview).Longish
Regarding your update. Yeah, I tried that. Using .singlePageContinuous it renders perfectly fast without any problems (even in the Simulator almost). But I don't want it to be continuous, unfortunately. It is extremely strange :DLongish
P
1

Please make sure to add values of .maxScaleFactor .minScaleFactor also as per your requirement and see if it makes any difference in the loading time. e.g.

.maxScaleFactor = 4.0;
.minScaleFactor = self.scaleFactorForSizeToFit;
Psychotic answered 8/4, 2018 at 13:45 Comment(2)
Just tried it. No change. Still slow. I think it shouldn't have any significant effects, anyway. I coded my own pdf viewer from the ground up and am able to render every pdf file full scale without any problems. Still, I'm curious why Apple's own PDFKit has problems with that.Longish
Quantm- I'm looking for a pdf viewer. Can you provide additional details regarding the one you made on some sample code?Runstadler
A
0

Did you try to change the interpolationQuality option ?

The interpolation quality for images drawn into the PDFView context.

Possible values are

  • none
  • low
  • high

Maybe you could try something like

pdfView.interpolationQuality = .low

or

pdfView.interpolationQuality = .none
Allhallowmas answered 5/7, 2018 at 15:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.