首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何知道何时使用PDFKit渲染

如何知道何时使用PDFKit渲染
EN

Stack Overflow用户
提问于 2019-04-10 08:29:08
回答 2查看 477关注 0票数 0

当屏幕上还没有显示PDF时,我正在尝试显示加载。问题是,我的加载总是在文档呈现之前停止,有时它不能花2到3秒,我需要知道什么时候PDF已经呈现来停止activyIndicator。是否可以使用PDFKIT?我的代码:

代码语言:javascript
复制
class PDFViewController: URLSessionDownloadDelegate {

    @IBOutlet weak var pdfView: PDFView!
    var pdfDocument: PDFDocument!

    override func viewDidLoad() {
        super.viewDidLoad()

        setupPDFView()
        setupNavigationBar()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        self.startLoading()
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        loadPDF()
    }

    private func loadPDF() {
        DispatchQueue.main.async {
            guard let url = URL(string: self.viewModel.pdfURL) else {
                self.showAlert(with: self.viewModel.strings.invalidInvoice)
                return
            }

            let urlSession = URLSession(configuration: .default, delegate: self, delegateQueue: OperationQueue())

            let downloadTask = urlSession.downloadTask(with: url)
            downloadTask.resume()
        }
    }

    private func setupPDFView() {
        self.pdfView.displayMode = .singlePageContinuous
        self.pdfView.autoScales = true
    }

    func startLoading() {

        guard let window = UIApplication.shared.keyWindow,
            !window.subviews.contains(where: { $0 is LoadingView }) else { return }

        let loadingView = LoadingView(frame: window.bounds)
        window.addSubview(loadingView)

        Thread.performUIUpdate {
            loadingView.startAnimation()
        }
    }

    func stopLoading() {

        guard let window = UIApplication.shared.keyWindow,
        let view = window.subviews.first(where: { $0 is LoadingView }),
        let loadingView = view as? LoadingView  else { return }

        Thread.performUIUpdate {
            loadingView.stopAnimation()
            loadingView.removeFromSuperview()
        }
    }

    func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
        DispatchQueue.main.async {
            self.pdfDocument = PDFDocument(url: location)

            if let pdfDocument = self.pdfDocument {
                self.pdfView.document = pdfDocument
            }

            self?.stopLoading()
        }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2019-04-10 08:49:40

这似乎是一个非常糟糕的想法:

代码语言:javascript
复制
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
    DispatchQueue.main.async {
        self.pdfDocument = PDFDocument(url: location)

问题是,当您离开当前线程并启动异步代码时,该方法结束,location处的临时文档可能会被销毁。我建议这样写:

代码语言:javascript
复制
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
    let pdf = PDFDocument(url: location)
    DispatchQueue.main.async {
        self.pdfDocument = pdf

我并不是说这会解决你的问题,但它似乎比你正在做的事情要危险得多。

至于你的实际问题,我建议注册一个通知,比如this one,看看它是否在正确的时间到达。

票数 2
EN

Stack Overflow用户

发布于 2019-09-20 10:45:55

要知道PDFView页面何时可见,必须观察PDFViewVisiblePagesChanged通知,将代码添加到该方法中。

代码语言:javascript
复制
NotificationCenter.default.addObserver(self, selector: #selector(pdfViewVisiblePagesChanged(sender:)), name: .PDFViewVisiblePagesChanged, object: nil)

@objc func pdfViewVisiblePagesChanged(sender: Notification) {

    //Add your code here
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55603064

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档