gpt4 book ai didi

swift - PMCreateSession 不创建打印 session

转载 作者:行者123 更新时间:2023-11-28 08:40:59 31 4
gpt4 key购买 nike

尝试为 OS X 应用获取可打印的矩形。似乎涉及创建 session ,然后是页面格式,验证格式等。代码编译,但从 PMCreateSession 获得 -50 的状态。我是否不正确地声明了 printSession ?通常不必与 UnsafeMutablePointers 打交道。

谢谢!

    let printSession: UnsafeMutablePointer<PMPrintSession> = nil
let pmPageFormat: UnsafeMutablePointer<PMPageFormat> = nil
var status = PMCreateSession(printSession)
status = PMCreatePageFormat(pmPageFormat)
status = PMSessionDefaultPageFormat(printSession.memory, pmPageFormat.memory)
let changed: UnsafeMutablePointer<DarwinBoolean> = nil
status = PMSessionValidatePageFormat(printSession.memory, pmPageFormat.memory, changed)
changed.destroy()
var pRect = PMRect()
status = PMGetAdjustedPageRect(pmPageFormat.memory, &pRect)

Swift.print("pRect \(pRect)")

status = PMRelease(pmPageFormat)
status = PMRelease(printSession)

最佳答案

Am I declaring printSession improperly?

可以按如下方式获取 PMPrintSession:

// create a C Null pointer of type PMPrintSession
let printSession = unsafeBitCast(0, to: PMPrintSession.self)
// pass by & converts PMPrintSession to UnsafeMutablePointer<PMPrintSession>
PMCreateSession(&printSession)

// recast printSession to release memory
PMRelease( PMObject(printSession) )

或者,可以从 Cocoa NSPrintInfo 访问 PMPrintSession:

let printInfo = NSPrintInfo.shared()
let printSession = PMPrintSession(printInfo.pmPrintSession())

Normally don't have to deal so much with UnsafeMutablePointers

有关使用 UnsafeMutablePointer 的更多信息,请参阅 StackOverflow "How to use UnsafeMutablePointer in Swift?"

有关在 Swift 中使用 Core Printing 的完整示例,请参阅 004.42Apple_CorePrintingExample在 GitHub 上。

关于swift - PMCreateSession 不创建打印 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36461514/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com