firebase - 是否可以在 SwiftUI 应用程序中显示 PDF 缩略图?

标签 firebase pdf swiftui firebase-storage

我想知道是否可以像 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/

相关文章:

ios - 如果满足 swift 条件,则显示 fcm 通知

android - 尝试在空对象引用上调用虚拟方法 com.google.firebase.iid.FirebaseInstanceId.getInstanceId()'

firebase - 安全的 Firebase 网络钩子(Hook)

c# - PDFsharp 保存到 MemoryStream

php - 无法将 php 页面转换为 PDF

ios - http请求登录成功后直接进入swiftUI中的新 View

android - 在 GCloud 中使用 Firebase 截屏时出错

pdf - DocuSign 和 EchoSign 使用的允许在线编辑、填写和签署 PDF 的 UI 技术是什么?

SwiftUI 多屏导航

plugins - 关闭 SwiftUI ColorPicker : "Connection to plugin interrupted while in use" 时出错