2

Good afternoon,

I am trying to take a snapshot of a PDF but I am facing some difficulties to access view's content on iOS 12.

In order to display the PDF's content, I've already used two different approaches:

  • UIDocumentInteractorController
  • Webview

On iOS 11, I couldn't already take a snapshot of a UIDocumentInteractorController view and the best answer that I could find was this one https://stackoverflow.com/a/13332623/2568889. In short, it says that the document viewer runs in an external process and on its own windows that the main app process doesn't have access to.

The WebView was the solution at that time, until iOS 12 came. While testing on real devices running iOS 12, I had the same issue of not being able to access the viewer's content while taking the snapshot. While inspecting the view hierarchy, it looks like we have a childviewcontroller (PDFHostViewController) that is the one rendering the actual view.

Please take into account that this issue is just happening for PDFs, on regular webpages is working fine!

Code used to take snapshot:

private extension UIView {

    func takeSnapshot() -> UIImage {
        let format = UIGraphicsImageRendererFormat()
        format.opaque = self.isOpaque
        let renderer = UIGraphicsImageRenderer(size: self.frame.size, format: format)
        return renderer.image { context in
            self.drawHierarchy(in: self.frame, afterScreenUpdates: true)
        }
    }

}

Note: I have also tried to use the native Webview.takeSnapshot(with:,completionHandler) of the web view but it just works for regular webpages, not PDFs

user2568889
  • 907
  • 1
  • 7
  • 8

1 Answers1

2

Maybe it works with Apple's PDFKit. As far as i know the PDFView is a subclass of UIView.

import PDFKit

@IBOutlet var pdfView: PDFView!

let pdfDocument = PDFDocument(url: url)
pdfView.document = pdfDocument

And then your extension as PDFView extension

Klinki
  • 368
  • 1
  • 11