import UIKit
extension UIView {
func toImage() -> UIImage {
UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.mainScreen().scale)
drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func sendPrint(sender: AnyObject) {
let testView = UIView(frame: CGRectMake(0, 0, 200, 200))
testView.backgroundColor = UIColor.blueColor()
let printcontroller = UIPrintInteractionController.sharedPrintController()
let printinfo = UIPrintInfo(dictionary:nil)
printinfo.jobName = "Printing an Image"
printinfo.outputType = UIPrintInfoOutputType.General
printcontroller.printInfo = printinfo
// printcontroller.printFormatter = webView.viewPrintFormatter()
printcontroller.showsNumberOfCopies = false
printcontroller.presentAnimated(true) { (_, isPrinted, error) in
if error == nil
{
if isPrinted
{
print ("Printed Successfully")
} else
{
print ("Printing failed")
}
}
}
}
}
我已将此代码写入AirPrint我UserInterface
的{{1}}。
但现在代码不再起作用了。
我收到运行时错误:“尝试显示没有打印源的打印选项(item / items / formatter / renderer)”