PMCreateSession不创建打印会话

时间:2016-04-06 20:19:03

标签: swift macos

尝试为OS X应用程序获取可打印的rect。似乎涉及创建会话,然后是页面格式,验证格式等。代码编译,但从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)

1 个答案:

答案 0 :(得分:0)

  

我是否正确宣布printSession?

可以按如下方式获得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 PMPrintSession访问NSPrintInfo

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

通常不必处理UnsafeMutablePointers

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

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