使用swift AirPrint UIView布局?

时间:2016-08-08 08:18:31

标签: swift xcode airprint

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

请帮助我,我在最近3天内坚持使用此代码 我只想从View生成图像,其中包含2或3 UIViewlabelsMy View

0 个答案:

没有答案