如何在 macOS 上打印带有文本和图像的 SwiftUI 视图?

发布于 2025-01-20 00:11:31 字数 2683 浏览 1 评论 0原文

我正在尝试在MacOS上打印SwiftUi视图的内容。根据该文档,通过给出nsprintoperation a nsviewnshostingview创建的 nsview 。 Swiftui视图的身体包含几种文本视图和图像视图,这些视图围绕着边界。所有文本都打印出来,但是图像和边界不可见。还需要其他东西来完成这项工作吗?

这是证明问题的样本。只需创建一个新的MacOS应用程序,将ContentView替换为下面的代码,然后在签名中启用打印&功能:

struct ContentView: View {
    
    var body: some View {
        VStack {
            Button("Print", action: self.onPrint )
            Divider()
            Print_Preview()
        }
    }
    
    private func onPrint() {
        let pi = NSPrintInfo.shared
        pi.topMargin = 0.0
        pi.bottomMargin = 0.0
        pi.leftMargin = 0.0
        pi.rightMargin = 0.0
        pi.orientation = .landscape
        pi.isHorizontallyCentered = false
        pi.isVerticallyCentered = false
        pi.scalingFactor = 1.0
                
        let rootView = Print_Preview()
        let view = NSHostingView(rootView: rootView)
        view.frame.size = CGSize(width: 300, height: 300)
        let po = NSPrintOperation(view: view, printInfo: pi)
        po.printInfo.orientation = .landscape
        po.showsPrintPanel = true
        po.showsProgressPanel = true
        
        po.printPanel.options.insert(NSPrintPanel.Options.showsPaperSize)
        po.printPanel.options.insert(NSPrintPanel.Options.showsOrientation)
        
        if po.run() {
            print("In Print completion")
        }
    }
    
    struct Print_Preview: View {
        var body: some View {
            VStack(alignment: .leading) {
                Text("Bordered Text Above Bordered Image")
                    .font(.system(size: 8))
                    .padding(5)
                    .border(Color.black, width: 2)
                Image(systemName: "printer")
                    .resizable()
                    .padding(5)
                    .border(Color.black, width: 2)
                    .frame(width: 100, height: 100)
                Text("Bordered Text Below Bordered Image")
                    .font(.system(size: 8))
                    .padding(5)
                    .border(Color.black, width: 2)
            }
            .padding()
            .foregroundColor(Color.black)
            .background(Color.white)
            .frame(width: 200, height: 200)
        }
    }

}

此外,这里是应用程序和打印面板的屏幕截图。

”打印面板屏幕截图

I'm trying to print the contents of a SwiftUI view on macOS. According to the documentation, this seems like it should be possible by giving the NSPrintOperation an NSView created by an NSHostingView. The SwiftUI view's body contains several Text views and an Image view, with borders around those views. All of the Text gets printed, however the Image and the borders are not visible. Is there something else needed to make this work?

Here is a sample to demonstrate the problem. Just create a new macOS App, replace the ContentView with the code below and then enable Printing in Signing & Capabilities:

struct ContentView: View {
    
    var body: some View {
        VStack {
            Button("Print", action: self.onPrint )
            Divider()
            Print_Preview()
        }
    }
    
    private func onPrint() {
        let pi = NSPrintInfo.shared
        pi.topMargin = 0.0
        pi.bottomMargin = 0.0
        pi.leftMargin = 0.0
        pi.rightMargin = 0.0
        pi.orientation = .landscape
        pi.isHorizontallyCentered = false
        pi.isVerticallyCentered = false
        pi.scalingFactor = 1.0
                
        let rootView = Print_Preview()
        let view = NSHostingView(rootView: rootView)
        view.frame.size = CGSize(width: 300, height: 300)
        let po = NSPrintOperation(view: view, printInfo: pi)
        po.printInfo.orientation = .landscape
        po.showsPrintPanel = true
        po.showsProgressPanel = true
        
        po.printPanel.options.insert(NSPrintPanel.Options.showsPaperSize)
        po.printPanel.options.insert(NSPrintPanel.Options.showsOrientation)
        
        if po.run() {
            print("In Print completion")
        }
    }
    
    struct Print_Preview: View {
        var body: some View {
            VStack(alignment: .leading) {
                Text("Bordered Text Above Bordered Image")
                    .font(.system(size: 8))
                    .padding(5)
                    .border(Color.black, width: 2)
                Image(systemName: "printer")
                    .resizable()
                    .padding(5)
                    .border(Color.black, width: 2)
                    .frame(width: 100, height: 100)
                Text("Bordered Text Below Bordered Image")
                    .font(.system(size: 8))
                    .padding(5)
                    .border(Color.black, width: 2)
            }
            .padding()
            .foregroundColor(Color.black)
            .background(Color.white)
            .frame(width: 200, height: 200)
        }
    }

}

Also, here are screenshots of the App and Print Panel.

Sample App screenshot

Print Panel screenshot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

念﹏祤嫣 2025-01-27 00:11:31

感谢@Willeke 的评论和指针,感谢@user2120275 提出了一个不同的问题,该问题恰好包含解决我的问题所需的技巧。解决方案是从 NSHostingView 返回的 NSView 创建一个 NSImageView,然后打印该 NSImageView 而不是原始的 NSView。

来工作:

view.frame.size = CGSize(width: 300, height: 300)
let po = NSPrintOperation(view: view, printInfo: pi)

我上面的示例代码可以通过将这两行替换为以下内容

let contentRect = NSRect(x: 0, y: 0, width: 300, height: 300)
view.frame.size = contentRect.size

let newWindow = NSWindow(
    contentRect: contentRect,
    styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
    backing: .buffered, defer: false)
newWindow.contentView = view

let myNSBitMapRep = newWindow.contentView!.bitmapImageRepForCachingDisplay(in: contentRect)!
newWindow.contentView!.cacheDisplay(in: contentRect, to: myNSBitMapRep)

let myNSImage = NSImage(size: myNSBitMapRep.size)
myNSImage.addRepresentation(myNSBitMapRep)

let nsImageView = NSImageView(frame: contentRect)
nsImageView.image = myNSImage

let po = NSPrintOperation(view: nsImageView, printInfo: pi)

Thanks to @Willeke for the comment with a pointer and to @user2120275 for asking a different question that happened to contain the trick needed to fix my problem. The solution is to create an NSImageView from the NSView returned by the NSHostingView, and then print that NSImageView instead of the original NSView.

My sample code above can be made to work by replacing these two lines:

view.frame.size = CGSize(width: 300, height: 300)
let po = NSPrintOperation(view: view, printInfo: pi)

with the following:

let contentRect = NSRect(x: 0, y: 0, width: 300, height: 300)
view.frame.size = contentRect.size

let newWindow = NSWindow(
    contentRect: contentRect,
    styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
    backing: .buffered, defer: false)
newWindow.contentView = view

let myNSBitMapRep = newWindow.contentView!.bitmapImageRepForCachingDisplay(in: contentRect)!
newWindow.contentView!.cacheDisplay(in: contentRect, to: myNSBitMapRep)

let myNSImage = NSImage(size: myNSBitMapRep.size)
myNSImage.addRepresentation(myNSBitMapRep)

let nsImageView = NSImageView(frame: contentRect)
nsImageView.image = myNSImage

let po = NSPrintOperation(view: nsImageView, printInfo: pi)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文