我的打印按钮如何打印8.5英寸×11英寸的图像? Swift 3,IOS

时间:2017-03-04 18:29:10

标签: ios printing swift3 uiprintinteractioncntrler

我试图直接从我的应用程序打印 - 它是一个清单。唯一的问题是当我打印时,图像将只填充打印纸的1/4。我希望图像能够填满8.5英寸×11英寸的纸张。

这是我的打印按钮:

    @IBAction func buttonAction2(_ sender: UIButton) {


    //Screenshot of view controller (minus status/Nav bar)
    let top: CGFloat = 46
    let bottom: CGFloat = 0
    let size = CGSize(width: view.frame.size.width, height: view.frame.size.height - top - bottom)
    UIGraphicsBeginImageContextWithOptions(size, false, 0)
    let context = UIGraphicsGetCurrentContext()!
    context.translateBy(x: 0, y: -top)
    view.layer.render(in: context)
    let snapshot = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    ///Print screenshot
    let printController = UIPrintInteractionController.shared
    let printInfo = UIPrintInfo(dictionary:nil)

    printInfo.jobName = "printing an image"
    printInfo.outputType = .photo

    printController.printInfo = printInfo
    printController.printingItem = snapshot
    printController.present(animated: true)  { (_, isPrinted, error) in if error == nil {
        if isPrinted {
            print("image is printed")
        }else{
            print("image is not printed")
        }
        }
    }

}

我可以将图像保存到相机胶卷并从那里打印,而不是直接从我的应用程序打印,而且效果很好。从我的相机胶卷打印时,图像会填满整个页面:

 //Save photo to camera roll
    UIImageWriteToSavedPhotosAlbum(snapshot!, nil, nil, nil)

我真的需要从应用程序打印。有没有办法实现这个目标?

2 个答案:

答案 0 :(得分:0)

您是否尝试在翻译后缩放上下文?

context.translateBy(x: 0, y: -top)
context.scaleBy(x: 2, y: 2)

答案 1 :(得分:0)

我明白了。我很傻。

使用:

printInfo.outputType = .general 

而不是:

printInfo.outputType = .photo