iosprintingswift3uiprintinteractioncntrler

How can my print button print images that are 8.5 inches by 11 inches? Swift 3, IOS


Im trying to print directly from my app - it's a checklist. Only problem is when i print, the image will only fill 1/4 of the printing paper. I want the image to fill the full 8.5 inch by 11 inch paper.

This is my print button:

    @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")
        }
        }
    }

}

I can save the image to my camera roll and print from there, instead of printing directly from my app and that works perfectly. The image fills the entire page, when printed from my camera roll:

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

I really need to print from the app, though. Is there any way to achieve this?


Solution

  • I figured it out. I'm silly.

    Use:

    printInfo.outputType = .general 
    

    Instead of:

    printInfo.outputType = .photo