快速查看预览仅在 MacOS 中显示模糊的第一页文档

Quick look preview only shows a blurred 1st page of documents in MacOS

我有一个 SwiftUI iOS 应用程序,我已将其用于 mac 催化剂。我正在使用 QuickLook Preview 在应用程序中显示 pdf、docx、图像和 pptx。渲染在 iOS 上运行良好,但在 MacOS 上,只有一页文档显示模糊。

这是我实现 QuickLook 的代码:

import SwiftUI
import QuickLook

struct PreviewController: UIViewControllerRepresentable {

@Binding var url: URL

func makeUIViewController(context: Context) -> QLPreviewController {
    let controller = QLPreviewController()
    controller.dataSource = context.coordinator
    return controller
}

func updateUIViewController(
    _ uiViewController: QLPreviewController, context: Context) {}


func makeCoordinator() -> Coordinator {
    return Coordinator(parent: self)
}

class Coordinator: QLPreviewControllerDataSource {
    
    let parent: PreviewController
    
    init(parent: PreviewController) {
        self.parent = parent
    }
    
    func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
        return 1
    }
    
    func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
        return parent.url as NSURL
    }
    
}
}

然后我在 sheet 的内容视图中调用它。 问题是我该怎么做才能在 MacOS 上正确呈现文档?

我终于找到了解决此 problem.The 技巧的可行方法,即在 navigationViewController 中显示快速预览。 这是代码:

struct PreviewControllerMac: UIViewControllerRepresentable {
@Binding var url: URL

func makeUIViewController(context: Context) -> UINavigationController {
    let controller = QLPreviewController()
    controller.dataSource = context.coordinator
    controller.delegate = context.coordinator
    let navigationController = UINavigationController(rootViewController: controller)
    return navigationController
}
func updateUIViewController(_ uiViewController: UINavigationController, context: Context) {}
func makeCoordinator() -> Coordinator {
    return Coordinator(parent: self)
}
class Coordinator: NSObject, QLPreviewControllerDelegate, QLPreviewControllerDataSource {
    let parent: PreviewControllerMac
    init(parent: PreviewControllerMac) {
        self.parent = parent
    }
    func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
        return 1
}
func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
return parent.url as NSURL
}
func previewController(_ controller: QLPreviewController, editingModeFor previewItem: QLPreviewItem) -> QLPreviewItemEditingMode {
return .updateContents
    }
}