我有一个我在Storyboard中创建的UIView,我想用它来打印一个表单,我已实例化它然后我调用View然后一切都开始工作,但是,当这发生在较小的筛选时项目,例如iPhone,它会切断屏幕所在的位置。我不知道我需要调整大小或什么来避免这种情况。现在这就是我用它来扩展它所以我有一个高质量(这个问题发生在我添加质量的规模之前)。我实例化时应该设置大小吗?
func generatePDF() {
let pdfData = NSMutableData()
let bounds = self.view.bounds
UIGraphicsBeginPDFContextToData(pdfData, bounds, nil)
UIGraphicsBeginPDFPage()
guard let pdfContext = UIGraphicsGetCurrentContext() else { return }
let rescale : CGFloat = 4
func scaler(v: UIView) {
if !v.isKindOfClass(UIStackView.self) {
v.contentScaleFactor = 8
}
for sv in v.subviews {
scaler(sv)
}
}
scaler(pdfView)
let bigSize = CGSize(width: pdfView.frame.size.width*rescale, height: pdfView.frame.size.width*rescale)
UIGraphicsBeginImageContextWithOptions(bigSize, true, 1)
let context = UIGraphicsGetCurrentContext()!
CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor)
CGContextFillRect(context, CGRect(origin: CGPoint(x: 0, y: 0), size: bigSize))
CGContextScaleCTM(context, rescale, rescale)
pdfView.layer.renderInContext(context)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
CGContextSaveGState(pdfContext)
CGContextTranslateCTM(pdfContext, pdfView.frame.origin.x, pdfView.frame.origin.y)
CGContextScaleCTM(pdfContext, 1/rescale, 1/rescale)
let frame = CGRect(origin: CGPoint(x: 0, y: 0), size: bigSize)
image!.drawInRect(frame)
CGContextRestoreGState(pdfContext)
let info : UIPrintInfo = UIPrintInfo(dictionary: nil)
info.orientation = UIPrintInfoOrientation.Portrait
info.outputType = UIPrintInfoOutputType.Grayscale
info.jobName = "Work Order"
if NSUserDefaults.standardUserDefaults().URLForKey("printer") != nil {
let printer = UIPrinter(URL: NSUserDefaults.standardUserDefaults().URLForKey("printer")!)
printer.contactPrinter { (available) in
if available {
let printInteraction = UIPrintInteractionController.sharedPrintController()
printInteraction.printingItem = image
printInteraction.printInfo = info
printInteraction.printToPrinter(printer, completionHandler: { (printerController, completed, error) in
if completed {
let alert = UIAlertController(title: "Printed!", message: nil, preferredStyle: .Alert)
self.presentViewController(alert, animated: true, completion: nil)
let delay = 1.0 * Double(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(time, dispatch_get_main_queue(), {
alert.dismissViewControllerAnimated(true, completion: {
self.dismissViewControllerAnimated(false, completion: nil)
})
})
}
})
}
}
} else {
self.dismissViewControllerAnimated(false, completion: nil)
let alert = UIAlertController(title: "Select Printer", message: "\nPlease select a printer in the More section and then try your print again", preferredStyle: .Alert)
let okayButton = UIAlertAction(title: "Okay", style: .Default, handler: nil)
alert.addAction(okayButton)
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
}
}
答案 0 :(得分:0)
尝试将UIView的边界设置为屏幕的边界,如下所示:
let screenSize: CGRect = UIScreen.main.bounds
view.frame = CGRect(x: 0.0, y: 0.0, width: screenSize.width, height: screenSize.height)