2017-03-04 154 views
0

我試圖直接從我的應用程序打印 - 這是一個清單。唯一的問題是當我打印時,圖像只會填滿打印紙的1/4。我希望圖像能夠填滿8.5英寸x 11英寸的紙張。我的打印按鈕如何打印8.5英寸×11英寸的圖像?斯威夫特3,IOS

這是我的打印按鈕:

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

雖然我確實需要從應用程序進行打印。有什麼辦法可以做到這一點?

回答

0

您是否嘗試在翻譯後縮放上下文?

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

的圖像是現在75%切斷(從它吹起來),並且仍然只填補了網頁的75%。 – DanielG

0

我想通了。我太愚蠢。

用途:

printInfo.outputType = .general 

相反的:

printInfo.outputType = .photo