0

我试图直接从我的应用程序打印 - 这是一个清单。唯一的问题是当我打印时,图像只会填满打印纸的 1/4。我希望图像填满 8.5 英寸 x 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)

不过,我真的需要从应用程序打印。有什么办法可以做到这一点?

4

2 回答 2

0

我想到了。我很笨。

利用:

printInfo.outputType = .general 

代替:

printInfo.outputType = .photo
于 2017-03-06T06:12:59.810 回答
0

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

context.translateBy(x: 0, y: -top)
context.scaleBy(x: 2, y: 2)
于 2017-03-04T22:30:39.990 回答