我在应用程序文档文件夹中存储了一个 PDF 文件。
我有一个 PDFView 作为我的视图控制器视图的子视图。我可以从文件中加载 PDF 并显示它。没问题。
但是,根据 PDF 的大小,PDFView 可能需要一段时间才能加载。
我正在使用 Objective-C。
这是我的代码:
- (void)loadDocument {
_pdfView.document = [_fileFromThirdParty pdfDocument];
}
_pdfView 的定义位置:
@property (strong, nonatomic) IBOutlet PDFView *pdfView;
我想在文档加载到视图中时执行一些操作。
因此,我们在 PDFView 中定义了 PDFDocument。检查这两个对象表明我们在页面更改等时发布了通知。但是当文档成功加载到视图中时,我找不到任何触发的内容。
我希望有一个像这样的委托方法:
- (void)pdfView:(PDFView *)pdfView pdfHasLoaded:(BOOL)pdfHasLoaded {
// Great - I can do something...!
}
我想向窗口添加深色视图(以显示活动),并在 PDF 加载后将其删除。我已经打败了谷歌和SO。没有什么那么明显。
需要明确的是,我不是从服务器上的 URL 中提取此内容。这只是从文档包中加载。
任何人有任何想法。
你可以这样做:
func loadPDF() {
showLoader()
DispatchQueue.global(qos: .background).async {
if let urlPDF = URL(string: "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf") {
let document = PDFDocument(url: urlPDF)
DispatchQueue.main.async(execute: {
self.pdfView.document = document
dismissLoader()
})
}
}
}
对于 SwiftUI,可以有一个视图模型实现,例如:
final class PDFViewModel: ObservableObject {
var url: String
@Published var documentDidLoad = false
var pdfDocument: PDFDocument? {
didSet {
DispatchQueue.main.async { [self] in
documentDidLoad = true
}
}
}
init(url: String = "REPLACE_ME") {
self.url = url
}
func loadPDFDocument() {
DispatchQueue.global(qos: .background).async { [self] in
pdfDocument = PDFDocument(url: NSURL(string: url)! as URL)!
}
}
}
然后可以从 SwiftUI 视图观察 documentDidLoad