av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

使用IOS AirPrint實(shí)現(xiàn)打印功能詳解

瀏覽:42日期:2022-09-16 17:35:50
內(nèi)容1.什么是AirPrint

其實(shí)就是將iOS(iphone,ipad)上的內(nèi)容,使用支持AirPrint的打印機(jī)打印出來(lái)。打印過(guò)程無(wú)線(xiàn)控制, 非常方便。

2.第一手資料

學(xué)習(xí)iOS, 第一手資料肯定非蘋(píng)果官方文檔莫屬.here。 (我下面敘述的內(nèi)容基本上是對(duì)文檔的總結(jié), 英語(yǔ)可以的建議直接看文檔。。。)

3.Printer Simulator,使用打印模擬器進(jìn)行測(cè)試

既然涉及打印功能,那么就需要有一臺(tái)支持AirPrint 功能的打印機(jī)進(jìn)行測(cè)試嘍,你沒(méi)有?沒(méi)關(guān)系!蘋(píng)果已經(jīng)為我們準(zhǔn)備好了模擬器。 這個(gè)模擬器在Xcode中沒(méi)有, 需要自己到官網(wǎng)下載

使用IOS AirPrint實(shí)現(xiàn)打印功能詳解

打印模擬器位置

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

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

使用IOS AirPrint實(shí)現(xiàn)打印功能詳解

AirPrint相關(guān)類(lèi)

UIPrintInteractionController 屬性:

UIPrintInfo *printInfo: 打印任務(wù)的信息。 UIPrintPaper * printPaper : 打印內(nèi)容的區(qū)域。 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è)活呀。 這里如果使用 swift的枚舉,就很容易理解了。

使用IOS AirPrint實(shí)現(xiàn)打印功能詳解

需要打印的內(nèi)容與相應(yīng)參數(shù)的對(duì)應(yīng)方式

6.打印流程 創(chuàng)建 UIPrintInteractionController 實(shí)例。 創(chuàng)建UIPrintInfo 實(shí)例。 并 配置參數(shù) output type(輸出類(lèi)型), print orientation(打印方向), job name(打印工作標(biāo)識(shí)), 然后賦值給UIPrintInteractionController 實(shí)例的 printInfo屬性。 給delegate 屬性賦值, 賦的值必須遵守 UIPrintInteractionControllerDelegate 協(xié)議。 這個(gè)代理可以 響應(yīng) printing options界面的顯示和消失, 打印工作的開(kāi)始和結(jié)束 等。 指定要打印的內(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è)。 最后就是顯示顯示出printing options 界面了。 方法:

在iPad上: presentFromBarButtonItem:animated:completionHandler:

或者 presentFromRect:inView:animated:completionHandler:;

在手機(jī)上: presentAnimated:completionHandler:

說(shuō)了這么多, 理論知識(shí)就介紹的差不多了, 下面通過(guò)代碼演示具體實(shí)現(xiàn)。

7.Printing Printer-Ready Content (打印準(zhǔn)備好的內(nèi)容)

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

對(duì)于 image來(lái)說(shuō), NSData, NSURL, UIImage, and ALAsset 類(lèi)型都可以的。 對(duì)于PDF, 只能使用 NSData, NSURL。 然后需要將這些數(shù)據(jù)實(shí)例直接賦值 給 UIPrintInteractionController實(shí)例的 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]; }}

通過(guò)在iPhone上測(cè)試, 顯示出的全部是英文的,不要擔(dān)心, 因?yàn)檫@是系統(tǒng)的控件,也就是說(shuō)系統(tǒng)會(huì)自動(dòng)幫你作國(guó)際化處理,你不用作任何事情!你唯一要作的事???將Info.plist文件中的第一項(xiàng) Localization native development region(CFBundleDevelopmentRegion)的值設(shè)為 China(zh_CN);

使用IOS AirPrint實(shí)現(xiàn)打印功能詳解

Printer Options顯示英文

使用IOS AirPrint實(shí)現(xiàn)打印功能詳解

將英文修改成中文

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

系統(tǒng)提供了三個(gè) Print Formatters類(lèi), 分別是:

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.

英文介紹已經(jīng)很詳細(xì)了, 就不??鋁耍 直接展示出打印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 界面上顯示的內(nèi)容打印出來(lái)。

- (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(頁(yè)渲染器)

這部分內(nèi)容是最復(fù)雜的了, 感覺(jué)不怎么用,暫且不深究了, 大家如果項(xiàng)目需要, 自己看文檔吧。

以上就是使用IOS AirPrint實(shí)現(xiàn)打印功能詳解的詳細(xì)內(nèi)容,更多關(guān)于IOS AirPrint打印功能的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: IOS
相關(guān)文章:
主站蜘蛛池模板: 国产精品久久久久久久免费大片 | 国产精品视频一二三区 | 久草在线 | 国产激情三区 | 在线欧美小视频 | 国产免费观看一级国产 | 精品国产一区二区三区在线观看 | 99精品欧美一区二区三区 | 不用播放器看的av | 国产精品久久久久久久久久久久久 | 综合久久99 | 成人久久久久久久久 | 亚洲欧美精 | 成人午夜性成交 | 午夜精品网站 | 久草在线高清 | 91精品久久久久久久久久小网站 | 午夜精品视频在线观看 | 日韩在线视频一区 | 色狠狠一区| 国产一区二区精品 | 国产欧美精品 | 欧州一区二区三区 | 日韩精品视频网 | 欧美自拍第一页 | 国产免费一区二区 | 亚洲精品国产区 | 亚洲午夜精品视频 | 国产精品免费一区二区三区四区 | 成人av一区二区在线观看 | 精品久久精品 | 日韩三区在线 | 日本三级电影免费观看 | 中文字幕视频在线 | 久热精品在线 | 亚洲一区 中文字幕 | 国产色网站 | 91成人在线视频 | 国产成人久久av免费高清密臂 | 国产视频导航 | 日本一道本视频 |