您的位置:首頁技術文章
文章詳情頁

使用IOS AirPrint實現打印功能詳解

瀏覽:19日期:2022-09-16 17:35:50
內容1.什么是AirPrint

其實就是將iOS(iphone,ipad)上的內容,使用支持AirPrint的打印機打印出來。打印過程無線控制, 非常方便。

2.第一手資料

學習iOS, 第一手資料肯定非蘋果官方文檔莫屬.here。 (我下面敘述的內容基本上是對文檔的總結, 英語可以的建議直接看文檔。。。)

3.Printer Simulator,使用打印模擬器進行測試

既然涉及打印功能,那么就需要有一臺支持AirPrint 功能的打印機進行測試嘍,你沒有?沒關系!蘋果已經為我們準備好了模擬器。 這個模擬器在Xcode中沒有, 需要自己到官網下載

使用IOS AirPrint實現打印功能詳解

打印模擬器位置

4.了解一下AirPrint可以打印的內容 an array of ready-to-print images and PDF documents: 一組圖片文件和PDF文件。 a single image or PDF document: 一張圖片或是一個pdf文件。、 an instance of any of the built-in print formatter classes: 打印格式化者的實例。(簡單文本,html文檔,某些View顯示的內容)。 a custom page renderer: 自定義頁渲染者。5.關于AirPrint的API

AirPrint的api包含 eight classes and one protocol。 下圖是它們之間的關系。(下面這張圖明白了, 那你基本就掌握了)。

使用IOS AirPrint實現打印功能詳解

AirPrint相關類

UIPrintInteractionController 屬性:

UIPrintInfo *printInfo: 打印任務的信息。 UIPrintPaper * printPaper : 打印內容的區域。 delegate: 遵守UIPrintInteractionControllerDelegate 協議的代理。 最重要的就是制定需要打印的內容: printingItem , printingItems, printFormatter, printPageRenderer。 四個屬性都是用來指定要打印的內容的。 這四個參數是互斥的, 也就是說只要一個賦值, 其他三個參數就得是nil. 很容易理解,一個打印任務, 不能同時干多個活呀。 這里如果使用 swift的枚舉,就很容易理解了。

使用IOS AirPrint實現打印功能詳解

需要打印的內容與相應參數的對應方式

6.打印流程 創建 UIPrintInteractionController 實例。 創建UIPrintInfo 實例。 并 配置參數 output type(輸出類型), print orientation(打印方向), job name(打印工作標識), 然后賦值給UIPrintInteractionController 實例的 printInfo屬性。 給delegate 屬性賦值, 賦的值必須遵守 UIPrintInteractionControllerDelegate 協議。 這個代理可以 響應 printing options界面的顯示和消失, 打印工作的開始和結束 等。 指定要打印的內容。 也就是指定 printingItem , printingItems, printFormatter, printPageRenderer. 參數的其中一個。 當你使用 printPageRenderer. 時情況會復雜一些。 你可以繪制每一頁的header, footer, 內容。 這是你需要自己計算頁數。 另外, 你也可以創建一個或多個 UIPrintFormatter實例, 通過 addPrintFormatter:startingAtPageAtIndex: 或者 printFormatters參數 賦值給 printPageRenderer.實例。 這種情況下不需要自己計算多少頁。 最后就是顯示顯示出printing options 界面了。 方法:

在iPad上: presentFromBarButtonItem:animated:completionHandler:

或者 presentFromRect:inView:animated:completionHandler:;

在手機上: presentAnimated:completionHandler:

說了這么多, 理論知識就介紹的差不多了, 下面通過代碼演示具體實現。

7.Printing Printer-Ready Content (打印準備好的內容)

AirPrint可以直接打印一些內容。 這些內容是 NSData, NSURL, UIImage, and ALAsset 類的實例, 但是這些實例的內容, 或者引用的類型(NSURL)必須是 image 或者pdf.

對于 image來說, NSData, NSURL, UIImage, and ALAsset 類型都可以的。 對于PDF, 只能使用 NSData, NSURL。 然后需要將這些數據實例直接賦值 給 UIPrintInteractionController實例的 printingItem 或者 printingItems 屬性。

打印pdf:

- (IBAction)printContent:(id)sender { UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController]; if (pic && [UIPrintInteractionController canPrintData: self.myPDFData] ) { pic.delegate = self;   UIPrintInfo *printInfo = [UIPrintInfo printInfo]; printInfo.outputType = UIPrintInfoOutputGeneral; printInfo.jobName = [self.path lastPathComponent]; printInfo.duplex = UIPrintInfoDuplexLongEdge; pic.printInfo = printInfo; pic.showsPageRange = YES; pic.printingItem = self.myPDFData;   void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) { self.content = nil; if (!completed && error) NSLog(@'FAILED! due to error in domain %@ with error code %u', error.domain, error.code); }; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { [pic presentFromBarButtonItem:self.printButton animated:YES completionHandler:completionHandler]; } else { [pic presentAnimated:YES completionHandler:completionHandler]; }}

通過在iPhone上測試, 顯示出的全部是英文的,不要擔心, 因為這是系統的控件,也就是說系統會自動幫你作國際化處理,你不用作任何事情!你唯一要作的事???將Info.plist文件中的第一項 Localization native development region(CFBundleDevelopmentRegion)的值設為 China(zh_CN);

使用IOS AirPrint實現打印功能詳解

Printer Options顯示英文

使用IOS AirPrint實現打印功能詳解

將英文修改成中文

8.Using Print Formatters (打印格式化者)

系統提供了三個 Print Formatters類, 分別是:

UIViewPrintFormatter—automatically lays out the content of a view over multiple pages. To obtain a print formatter for a view, call the view’s viewPrintFormatter method. Not all built-in UIKit classes support printing. Currently, only the view classes UIWebView, UITextView, and MKMapView know how to draw their contents for printing. View formatters should not be used for printing your own custom views. To print the contents of a custom view, use a UIPrintPageRenderer instead. UISimpleTextPrintFormatter—automatically draws and lays out plain-text documents. This formatter allows you to set global properties for the text, such a font, color, alignment, and line-break mode. UIMarkupTextPrintFormatter—automatically draws and lays out HTML documents.

英文介紹已經很詳細了, 就不??鋁耍 直接展示出打印HTML文檔的代碼:

- (IBAction)printContent:(id)sender { UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController]; pic.delegate = self;   UIPrintInfo *printInfo = [UIPrintInfo printInfo]; printInfo.outputType = UIPrintInfoOutputGeneral; printInfo.jobName = self.documentName; pic.printInfo = printInfo;   UIMarkupTextPrintFormatter *htmlFormatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:self.htmlString]; htmlFormatter.startPage = 0; htmlFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); // 1 inch margins pic.printFormatter = htmlFormatter; pic.showsPageRange = YES;   void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) { if (!completed && error) { NSLog(@'Printing could not complete because of error: %@', error); } }; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { [pic presentFromBarButtonItem:sender animated:YES completionHandler:completionHandler]; } else { [pic presentAnimated:YES completionHandler:completionHandler]; }}

將UIWebView 界面上顯示的內容打印出來。

- (void)printWebPage:(id)sender { UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController]; void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) { if(!completed && error){ NSLog(@'FAILED! due to error in domain %@ with error code %u', error.domain, error.code); } }; UIPrintInfo *printInfo = [UIPrintInfo printInfo]; printInfo.outputType = UIPrintInfoOutputGeneral; printInfo.jobName = [urlField text]; printInfo.duplex = UIPrintInfoDuplexLongEdge; controller.printInfo = printInfo; controller.showsPageRange = YES;   UIViewPrintFormatter *viewFormatter = [self.myWebView viewPrintFormatter]; viewFormatter.startPage = 0; controller.printFormatter = viewFormatter;   if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { [controller presentFromBarButtonItem:printButton animated:YES completionHandler:completionHandler]; }else [controller presentAnimated:YES completionHandler:completionHandler];}9.Using a Page Renderer(頁渲染器)

這部分內容是最復雜的了, 感覺不怎么用,暫且不深究了, 大家如果項目需要, 自己看文檔吧。

以上就是使用IOS AirPrint實現打印功能詳解的詳細內容,更多關于IOS AirPrint打印功能的資料請關注好吧啦網其它相關文章!

標簽: IOS
相關文章:
国产综合久久一区二区三区