swiftcocoansprintoperationnsprintinfo

Print NSImage in Correct Size


I am trying to print an NSImage but am having trouble getting it to be to proper size. I want the NSImage to be printed as a 4"x6" photo on any paper large enough. Here's the code I have now.

var printView = NSImageView(frame: NSRect(x: 0, y: 0, width: 72*4, height: 72*6))
var printInfo = NSPrintInfo()

printInfo.paperSize = NSSize(width: 72*4, height: 72*6)
printInfo.bottomMargin = 10
printInfo.topMargin = 10
printInfo.leftMargin = 10
printInfo.rightMargin = 10

printView.image = generateImage()
let printOp = NSPrintOperation(view: printView, printInfo: printInfo)
printOp.run()

Solution

  • This seems to fix my problem

    var printView = NSImageView(frame: NSRect(x: 0, y: 0, width: 72*4, height: 72*6))
    var printInfo = NSPrintInfo()
    
    printInfo.bottomMargin = 0
    printInfo.topMargin = 0
    printInfo.leftMargin = 0
    printInfo.rightMargin = 0
    
    printView.image = generateImage()
    let printOp = NSPrintOperation(view: printView, printInfo: printInfo)
    printOp.run()