欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2021年04月13日 10:06:40   作者:旺仔餅餅  
這篇文章主要介紹了使用IOS AirPrint實(shí)現(xiàn)打印功能詳解,想了解無線打印的同學(xué),一定要看一下

內(nèi)容

1.什么是AirPrint

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

2.第一手資料

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

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

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

打印模擬器位置

4.了解一下AirPrint可以打印的內(nèi)容

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

5.關(guān)于AirPrint的API

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

AirPrint相關(guān)類

UIPrintInteractionController 屬性:

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

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

6.打印流程

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

    在iPad上: presentFromBarButtonItem:animated:completionHandler:

    或者 presentFromRect:inView:animated:completionHandler:;

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

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

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

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

對(duì)于 image來說, NSData, NSURL, UIImage, and ALAsset 類型都可以的。 對(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];

    }

}

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

Printer Options顯示英文

將英文修改成中文

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

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

  1. 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.
  2. 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.
  3. 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)容打印出來。

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

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

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

相關(guān)文章

最新評(píng)論