我已经实现了一个自定义 PDFView,它可以从云端和本地(如果可用)加载 pdf 文件。对于本地实例,一切加载速度都很快,但是当 url 不是本地的,即来自服务器时,可能需要一段时间,我想在 PDFView 加载文件时添加一个 UIActivityIndicator,有没有办法让我们知道一个代表或一个通知来听取以跟踪这个?
我的实现基本上是这样的:
let url = ReportsRepository.shared.getReportUrl(id: "1234")
self.pdfView.document = PDFDocument(url: url)
在此之后,如果 URL 来自服务器,应用程序似乎会卡住,所以我需要在此处添加一个 UIActivityIndicator,问题是我如何使用 PDFKit 停止它?
最佳答案
我已经完成了后台队列的使用。这是我的代码。
//Some loading view here
DispatchQueue.global(qos: .background).async {
if let url = URL(string: self.selectedWebUrl) {
if let pdfDocument = PDFDocument(url: url) {
DispatchQueue.main.async {
//Load document and remove loading view
self.pdfView.displayMode = .singlePageContinuous
self.pdfView.autoScales = true
self.pdfView.displayDirection = .vertical
self.pdfView.document = pdfDocument
}
} else {
DispatchQueue.main.async {
//not able to load document from pdf
//Remove loading view
}
}
} else {
DispatchQueue.main.async {
//Wrong Url show alert or something
//Remove loading view
}
}
}
关于ios - 是否有特定的方法来监听 PDFDocument 或 PDFView url 加载完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50527161/