我想知道是否可以像 UIKit 一样在 SwiftUI 应用程序中显示 pdf 文件的缩略图?如果是这样,怎么做到的?
我希望能够从 Firebase 存储中获取 PDF 文件,然后在应用中显示这些 PDF。
我没能找到描述这个用例的任何内容...
最佳答案
要在 SwiftUI 中显示 PDF 缩略图,您需要包装 PDFThumbnailView
,它是 UIViewRepresentable
中的 UIView
。
类似于:
import PDFKit
import SwiftUI
struct PDFThumbnailRepresented : UIViewRepresentable {
var pdfView : PDFView
func makeUIView(context: Context) -> PDFThumbnailView {
let thumbnail = PDFThumbnailView()
thumbnail.pdfView = pdfView
thumbnail.thumbnailSize = CGSize(width: 100, height: 100)
thumbnail.layoutMode = .horizontal
return thumbnail
}
func updateUIView(_ uiView: PDFThumbnailView, context: Context) {
//do any updates you need
//you could update the thumbnailSize to the size of the view here if you want, for example
//uiView.thumbnailSize = uiView.bounds.size
}
}
您可以通过传递 PDFView
来使用它:PDFThumbnailRepresented(pdfView: pdfView)
要创建您的 PDFView,您需要获取数据或 url 并像这样创建它:
guard let path = Bundle.main.url(forResource: "example", withExtension: "pdf") else { return }
if let document = PDFDocument(url: path) {
pdfView.document = document
}
请注意,上面的示例是否在您应用的 Bundle
中。你关于从 Firebase 存储加载它的问题超出了这个问题的范围,我假设它只是在 SwiftUI 中显示 PDF 缩略图。
有关加载/创建 PDFThumbnailView 的更多资源,不包括像我上面那样包装它:https://www.hackingwithswift.com/example-code/libraries/how-to-show-pdf-thumbnails-using-pdfthumbnailview
关于firebase - 是否可以在 SwiftUI 应用程序中显示 PDF 缩略图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65733269/