我的打印按钮如何打印 8.5 英寸 x 11 英寸的图像? Swift3、IOS
How can my print button print images that are 8.5 inches by 11 inches? Swift 3, IOS
我正在尝试直接从我的应用程序打印 - 这是一个清单。唯一的问题是当我打印时,图像只会填满打印纸的 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)
不过,我确实需要从应用程序打印。有什么办法可以实现吗?
你试过翻译后缩放上下文吗?
context.translateBy(x: 0, y: -top)
context.scaleBy(x: 2, y: 2)
我明白了。我傻了
使用:
printInfo.outputType = .general
而不是:
printInfo.outputType = .photo
我正在尝试直接从我的应用程序打印 - 这是一个清单。唯一的问题是当我打印时,图像只会填满打印纸的 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)
不过,我确实需要从应用程序打印。有什么办法可以实现吗?
你试过翻译后缩放上下文吗?
context.translateBy(x: 0, y: -top)
context.scaleBy(x: 2, y: 2)
我明白了。我傻了
使用:
printInfo.outputType = .general
而不是:
printInfo.outputType = .photo