1.?什么是AirPrint
????????AirPrint?是可以讓?xiě)?yīng)?用軟件通過(guò)?Apple?的?無(wú)驅(qū)動(dòng)程序打印體系結(jié)構(gòu),創(chuàng)建?無(wú)損打印輸出的?Apple?技術(shù)。
2.?使?用要求
????要使?用?AirPrint,您需要下列列任?一裝有最新版本的?iOS?的設(shè)備:
????iPad(所有機(jī)型)
????iPhone(3GS?或更更新機(jī)型)
????iPod touch(第?3?代或更更新機(jī)型)
????????還需要一臺(tái)本?文中列列出的?支持?AirPrint?的打印機(jī)。AirPrint 不?持下文中未列出的打印機(jī)。
? ?不支持AirPrint的打印機(jī)適配,如何設(shè)置實(shí)現(xiàn)airprint無(wú)線打印。
3.?可以打印的內(nèi)容
????3.1 an array of ready-to-print images and PDF documents: ?一組圖?片?文件和PDF?文件。
????3.2 a single image or PDF document: ?一張圖?片或是?一個(gè)pdf文件。
????3.3 an instance of any of the built-in print formatter classes:打印格式化者的實(shí)例例。(簡(jiǎn)單?文本,html?文檔,某些View顯示的內(nèi)容)。
????3.4 a custom page renderer: ?自定義?頁(yè)渲染者
4.?關(guān)于AirPrint的API

AirPrint相關(guān)類(lèi)
UIPrintInteractionController 屬性:
????????UIPrintInfo *printInfo: 打印任務(wù)的信息。
????????UIPrintPaper * printPaper : 打印內(nèi)容的區(qū)域。
????????delegate: 遵守UIPrintInteractionControllerDelegate 協(xié)議的代理
UIPrintInteractionController
????????UIPrintInteractionController是IOS中央印刷類(lèi)。它的共享實(shí)例代表一個(gè)打印作業(yè)。打印作業(yè),包括其印刷相關(guān)的信息和選項(xiàng),如輸出類(lèi)型,作業(yè)名稱(chēng),紙張大小和方向,要打印的內(nèi)容。
????????printInfo UIPrintInfo:之前所述的打印任務(wù)的配置。
????????printPaper UIPrintPaper:紙張類(lèi)型的物理和打印尺寸的一個(gè)簡(jiǎn)單的類(lèi)型描述;除了專(zhuān)門(mén)的應(yīng)用程序,這將由 UIKit 處理。
????????showsNumberOfCopies Bool:當(dāng)值為 true 時(shí),讓用戶(hù)選擇拷貝的份數(shù)。
????????showsPageRange Bool:當(dāng)值為 true 時(shí),讓用戶(hù)從打印源中選擇一個(gè)子范圍。這只在多頁(yè)內(nèi)容時(shí)有用,它默認(rèn)關(guān)閉了圖像。
????????showsPaperSelectionForLoadedPapers Bool:當(dāng)值為 true 并且所選擇的打印機(jī)有多個(gè)紙張選項(xiàng)時(shí),用戶(hù)界面將讓用戶(hù)選擇用于打印的紙張。
UIPrintInfo?
????????一個(gè)UIPrintInfo對(duì)象封裝了有關(guān)打印作業(yè)的信息,包括打印機(jī)標(biāo)識(shí),作業(yè)名稱(chēng),輸出類(lèi)型(照片,正常,灰階),方向(縱向或橫向),和任何選定的雙工模式。
????????jobName String:此打印任務(wù)的名稱(chēng)。這個(gè)名字將被顯示在設(shè)備的打印中心,對(duì)于有些打印機(jī)則顯示在液晶屏上。
????????orientation UIPrintInfoOrientation:.Portrait (默認(rèn)值)或 .Landscape,如果你打印的內(nèi)容有一個(gè)內(nèi)置的方向值,如 PDF,這個(gè)屬性將被忽略。
????????duplex UIPrintInfoDuplex:.None、.ShortEdge 或 .LongEdge。short- 和 long- 的邊界設(shè)置指示如何裝訂雙面頁(yè)面,而 .None 不支持雙面打印
????????outputType UIPrintInfoOutputType:給 UIKit 提供要打印內(nèi)容的類(lèi)型提示??梢允且韵氯我庖粋€(gè):
????????????.General(默認(rèn)):文本和圖形混合類(lèi)型;允許雙面打印。
????????????.Grayscale:如果你的內(nèi)容只包括黑色文本,那么該類(lèi)型比 .General 更好。
????????????.Photo:彩色或黑白圖像;禁用雙面打印,更適用于圖像媒體的紙張類(lèi)型。
????????????.PhotoGrayscale:對(duì)于僅灰度的圖像,根據(jù)打印機(jī)的不同,該類(lèi)型可能比 .Photo 更好。
????????printerID String?:一個(gè)特定的打印機(jī)的 ID,當(dāng)用戶(hù)通過(guò) UI 選擇過(guò)打印機(jī)并且保存它作為未來(lái)打印預(yù)設(shè)之后,你才能得到這個(gè)類(lèi)型
UIPrintPaper * printPaper : 打印內(nèi)容的區(qū)域。
????????UIPrintPaper類(lèi)的一個(gè)實(shí)例封裝使用的紙張尺寸,打印作業(yè),并在其中的內(nèi)容可以打印的矩形。
delegate: 遵守UIPrintInteractionControllerDelegate 協(xié)議的代理。
最重要的就是制定需要打印的內(nèi)容:?
printingItem,printingItems,printFormatter,printPageRenderer。?
四個(gè)屬性都是用來(lái)指定要打印的內(nèi)容的。 這四個(gè)參數(shù)是互斥的, 也就是說(shuō)只要一個(gè)賦值, 其他三個(gè)參數(shù)就得是nil. 很容易理解,一個(gè)打印任務(wù), 不能同時(shí)干多個(gè)活呀。
????????printingItem AnyObject! 或 printingItems [AnyObject]!:最基本的等級(jí),控制器只需要已經(jīng)可打印(圖像和 PDF 文件)的內(nèi)容,并將它們發(fā)送到打印機(jī)。
????????printFormatter UIPrintFormatter:更高等級(jí),你可以在應(yīng)用程序內(nèi)使用一個(gè) UIPrintFormatter 的子類(lèi)來(lái)對(duì)內(nèi)容進(jìn)行格式化,然后傳給 UIPrintInteractionController。你已經(jīng)做了一些格式化,剩下的大部分事情打印 API 會(huì)處理。
????????printPageRenderer UIPrintPageRenderer:最高級(jí)別,你可以創(chuàng)建 UIPrintPageRenderer 的一個(gè)自定義子類(lèi),結(jié)合頁(yè)面格式和自己的繪圖程序來(lái)繪制頁(yè)眉、頁(yè)腳和頁(yè)面內(nèi)容。
5. 打印流程
????5.1????創(chuàng)建 UIPrintInteractionController 實(shí)例。
? ??5.2?????創(chuàng)建UIPrintInfo 實(shí)例。
????????????一個(gè)UIPrintInfo對(duì)象封裝了有關(guān)打印作業(yè)的信息,包括打印機(jī)標(biāo)識(shí),作業(yè)名稱(chēng),輸出類(lèi)型(照片,正常,灰階),方向(縱向或橫向),和任何選定的雙工模式,。打印系統(tǒng)打印時(shí),使用此信息。例如:output type(輸出類(lèi)型), print orientation(打印方向), job name(打印工作標(biāo)識(shí)), 然后賦值給UIPrintInteractionController 實(shí)例的 printInfo屬性。
????5.3????給delegate 屬性賦值, 賦的值必須遵守?
????????UIPrintInteractionControllerDelegate 協(xié)議。 這個(gè)代理可以 響應(yīng) printing options界面的顯示和消失, 打印工作的開(kāi)始和結(jié)束 等。
????5.4????指定要打印的內(nèi)容。 也就是指定 printingItem , printingItems, printFormatter, printPageRenderer. 參數(shù)的其中一個(gè)。
????????當(dāng)你使用 printPageRenderer. 時(shí)情況會(huì)復(fù)雜一些。 你可以繪制每一頁(yè)的header, footer, 內(nèi)容。 這是你需要自己計(jì)算頁(yè)數(shù)。 另外, 你也可以創(chuàng)建一個(gè)或多個(gè) UIPrintFormatter實(shí)例, 通過(guò) addPrintFormatter:startingAtPageAtIndex: 或者 printFormatters參數(shù) 賦值給 printPageRenderer.實(shí)例。 這種情況下不需要自己計(jì)算多少頁(yè)。
????5.5????最后就是顯示顯示出printing options 界面了。
在iPad上: presentFromBarButtonItem:animated:completionHandler: 或者 presentFromRect:inView:animated:completionHandler:;
在手機(jī)上: presentAnimated:completionHandler:
- (IBAction)print1:(id)sender {
? ? UIPrintInteractionController *printer = [UIPrintInteractionController sharedPrintController];
? ? printer.delegate=self;
? ? //配置打印信息
? ? UIPrintInfo *Pinfo = [UIPrintInfo printInfo];
? ? Pinfo.outputType = UIPrintInfoOutputGeneral;//可打印文本、圖形、圖像
? ? Pinfo.jobName = @"Print for name";//可選屬性,用于在打印中心中標(biāo)識(shí)打印作業(yè)
? ? Pinfo.duplex = UIPrintInfoDuplexLongEdge;//雙面打印繞長(zhǎng)邊翻頁(yè),NONE為禁止雙面
? ? Pinfo.orientation = UIPrintInfoOrientationPortrait;//打印縱向還是橫向
? ? Pinfo.outputType = UIPrintInfoOutputGeneral;
? ? //? ? Pinfo.printerID = @"";指定默認(rèn)打印機(jī),也可以使用UIPrintInteractionControllerDelegate
? ? printer.printInfo= Pinfo;
? ? UIViewPrintFormatter *printFormatter = [self.imageV viewPrintFormatter];
? ? printer.printFormatter= printFormatter;
? ? printer.showsNumberOfCopies = NO;// 拷貝數(shù)量顯示
? ? printer.showsPaperSelectionForLoadedPapers = YES;
? ? //printer.showsPageRange = NO;// 讓用戶(hù)從打印源中選擇一個(gè)子范圍。這只在多頁(yè)內(nèi)容時(shí)有用
? ? CGRect rect = self.view.frame;
? ? if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
?? // iPhone調(diào)用這個(gè)方法 ? ? ?
[printer presentAnimated:YES completionHandler:^(UIPrintInteractionController* _NonnullprintInteractionController,BOOL completed,NSError* _Nullableerror) {
? ? ? ? ? ? if(!completed && error) {
? ? ? ? ? ? ? ? NSLog(@"Error");
????????????}
? ? ? ? }];
? ? }
? ? if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
? ? ? ? ? ? // iPad使用這個(gè)方法[printer presentFromRect:rect inView:self.view animated:YES completionHandler:^(UIPrintInteractionController* _NonnullprintInteractionController, BOOL completed,NSError* _Nullableerror) {
? ? ? ? ? ? if(!completed && error) {
? ? ? ? ? ? ? ? NSLog(@"Error");
? ? ? ? ? ? }
? ? ? ? }];
? ? }
}
ps:plist中設(shè)置打印頁(yè)面顯示為中文:
key:Localization native development region?
value:China
參考資料
百度百科:https://baike.baidu.com/item/Airprint
蘋(píng)果官方文檔:https://developer.apple.com/airprint
個(gè)人博客: