使用PageViewController的PDFKit-PDFView页面切换渲染缓慢求助
I've implemented a PDFView using PageViewController with the following code:
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: [:])
When swiping to the next page, the page appears blurry for about half a second before rendering clearly. This is hurting the user experience. I'm wondering if preloading pages can fix this issue, but I haven't found a solution yet.
Great question—preloading adjacent pages is absolutely a valid approach to fix this blurry transition issue, and there are a few concrete ways to implement it with PDFView and its underlying components. Here's how you can tackle it:
1. Pre-render adjacent PDF pages upfront
The blur happens because the page is being rendered on-the-fly during the swipe. You can pre-render the next (and previous) pages in the background to ensure they're ready when the user swipes.
Use PDFDocument's page(at:) method to get target pages, then force their rendering by accessing their underlying reference or drawing them to an offscreen context. Example:
// Get current page index guard let currentPage = pdfView.currentPage, let currentIndex = pdfDoc.index(for: currentPage) else { return } // Preload next page if currentIndex + 1 < pdfDoc.pageCount { let nextPage = pdfDoc.page(at: currentIndex + 1)! // Force rendering by accessing the page's CGPDFPageRef _ = nextPage.pageRef // Alternatively, draw to an offscreen context to cache it let renderer = UIGraphicsImageRenderer(size: nextPage.bounds(for: .mediaBox).size) _ = renderer.image { context in nextPage.draw(with: .mediaBox, to: context.cgContext) } } // Preload previous page (optional, for backward swipes) if currentIndex > 0 { let prevPage = pdfDoc.page(at: currentIndex - 1)! _ = prevPage.pageRef }
Trigger this preloading whenever the current page changes (e.g., in the pdfViewDidChangeCurrentPage(_:) delegate method) or when the user starts a swipe gesture.
2. Take control of the PageViewController
When using pdfView.usePageViewController(true, ...), the system manages page view controllers under the hood. For more control, implement your own UIPageViewController:
- Create a custom
UIPageViewControllersubclass. - For each PDF page, make a
UIViewControllercontaining aPDFViewconfigured to display that specific page. - In the data source methods (
viewControllerBeforeViewController(_:)andviewControllerAfterViewController(_:)), return pre-created view controllers for adjacent pages, and preload the ones beyond that in the background.
This way, when the user swipes, the target view controller's PDFView has already rendered the page, eliminating blur.
3. Adjust rendering priority
Boost the PDFView's rendering priority to allocate more resources to page rendering during transitions:
pdfView.displayPriority = 1.0 // Max priority
This helps reduce the blurriness duration by prioritizing page rendering over other tasks.
Bonus: Disable lazy rendering (for small PDFs)
By default, PDFView uses lazy rendering to only show visible page parts. For smaller PDFs, disable this to render full pages upfront:
pdfView.displaysAsBook = false // Adjust if using book mode pdfView.maxScaleFactor = pdfView.minScaleFactor // Lock scaling to force full-page render
Note this uses more memory, so only use it for PDFs with a reasonable page count.
Give these approaches a try—pre-rendering adjacent pages should significantly reduce or eliminate the blurry transition you're seeing.
内容的提问来源于stack exchange,提问作者SwiftedMind

