swift - NSView 到 PDF 和 PNG : Why is the outcome so different?

标签 swift macos cocoa draw nsview

我正在尝试将 NSView 保存到 PNG。 我从 NSView 开始,然后为 PNG 调用 dataWithPDFcacheDisplay。执行这两项操作的代码如下所示。

guard view.lockFocusIfCanDraw() else {
    assert (false)
    return
}
let pdfData = view.dataWithPDF(inside: rect)
guard let imgData = view.bitmapImageRepForCachingDisplay(in: rect) else {
    assert(false)
}
view.cacheDisplay(in: rect, to: imgData)
view.unlockFocus()
try pdfData.write(to: pdfName, options: .atomic)
let pngData = imgData.representation(using: .png, properties: [:])
try pngData!.write(to: pngName, options: .atomic)

到目前为止,还不错。然而,这是不同的结果。 PDF(正确!) correct PDF output 这是 PNG 输出。可以看到,不包括 subview 。箭头作为 view 的一部分绘制 enter image description here

为什么结果如此不同?

非常感谢!

最佳答案

好的,我找到了答案。感谢“View Debugging”,我看到 subview 使用了一个层(self.wantsLayer = true)。图层并没有进入 PNG,而是进入 PDF。不确定这是错误还是功能。但是,现在我可以修复 PNG 输出。

关于swift - NSView 到 PDF 和 PNG : Why is the outcome so different?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49494768/

相关文章:

objective-c - 使用 ParseKit 标记化和 DelimitedString

Swift NSSound 静音播放?

arrays - 如何使用 Swift3 删除 Array 中的特定 NSObject

swift - 如何从 Swift 运行 JXA?

wordpress - 本地主机:8080始终重定向到本地主机:3000

linux - 搜索所有文件和所有文件

objective-c - 在 Objective C 中加载表单时将光标焦点设置在 TextField 上

ios - 应用程序处理失败并显示消息 "This build is invalid."

ios - 在 SWIFT 中使用 FMDB 访问外部数据库

macos - 授权 XPC 连接