热门标签 | HotTags
当前位置:  开发笔记 > 前端 > 正文

使用IOSAirPrint实现打印功能详解

这篇文章主要介绍了使用IOSAirPrint实现打印功能详解,想了解无线打印的同学,一定要看一下

内容

1.什么是AirPrint

其实就是将iOS(iphone,ipad)上的内容,使用支持AirPrint的打印机打印出来。打印过程无线控制, 非常方便。

2.第一手资料

学习iOS, 第一手资料肯定非苹果官方文档莫属.
here。 (我下面叙述的内容基本上是对文档的总结, 英语可以的建议直接看文档。。。)

3.Printer Simulator,使用打印模拟器进行测试

既然涉及打印功能,那么就需要有一台支持AirPrint 功能的打印机进行测试喽,你没有?没关系!苹果已经为我们准备好了模拟器。 这个模拟器在Xcode中没有, 需要自己到官网下载

打印模拟器位置

4.了解一下AirPrint可以打印的内容

  1. an array of ready-to-print images and PDF documents: 一组图片文件和PDF文件。
  2. a single image or PDF document: 一张图片或是一个pdf文件。、
  3. an instance of any of the built-in print formatter classes: 打印格式化者的实例。(简单文本,html文档,某些View显示的内容)。
  4. a custom page renderer: 自定义页渲染者。

5.关于AirPrint的API

AirPrint的api包含 eight classes and one protocol。 下图是它们之间的关系。(下面这张图明白了, 那你基本就掌握了)。

AirPrint相关类

UIPrintInteractionController 属性:

  1. UIPrintInfo *printInfo: 打印任务的信息。
  2. UIPrintPaper * printPaper : 打印内容的区域。
  3. delegate: 遵守UIPrintInteractionControllerDelegate 协议的代理。
  4. 最重要的就是制定需要打印的内容: printingItem , printingItems, printFormatter, printPageRenderer。 四个属性都是用来指定要打印的内容的。 这四个参数是互斥的, 也就是说只要一个赋值, 其他三个参数就得是nil. 很容易理解,一个打印任务, 不能同时干多个活呀。 这里如果使用 swift的枚举,就很容易理解了。

需要打印的内容与相应参数的对应方式

6.打印流程

  1. 创建 UIPrintInteractionController 实例。
  2. 创建UIPrintInfo 实例。 并 配置参数 output type(输出类型), print orientation(打印方向), job name(打印工作标识), 然后赋值给UIPrintInteractionController 实例的 printInfo属性。
  3. 给delegate 属性赋值, 赋的值必须遵守 UIPrintInteractionControllerDelegate 协议。 这个代理可以 响应 printing options界面的显示和消失, 打印工作的开始和结束 等。
  4. 指定要打印的内容。 也就是指定 printingItem , printingItems, printFormatter, printPageRenderer. 参数的其中一个。
  5. 当你使用 printPageRenderer. 时情况会复杂一些。 你可以绘制每一页的header, footer, 内容。 这是你需要自己计算页数。 另外, 你也可以创建一个或多个 UIPrintFormatter实例, 通过 addPrintFormatter:startingAtPageAtIndex: 或者 printFormatters参数 赋值给 printPageRenderer.实例。 这种情况下不需要自己计算多少页。
  6. 最后就是显示显示出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);

Printer Options显示英文

将英文修改成中文

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

系统提供了三个 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.

英文介绍已经很详细了, 就不啰嗦了, 直接展示出打印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打印功能的资料请关注其它相关文章!


推荐阅读
  • 篇首语:本文由编程笔记#小编为大家整理,主要介绍了预加载多个本地WebView相关的知识,希望对你有一定的参考价值。 ... [详细]
  • 今天凌晨开始苹果正式开始了iOS10的推送工作,使用iPhone和iPad的用户可 ... [详细]
  • imnewtotheswiftandxcodeworld,soimhavingaproblemtryingtointegrateapackagetomypro ... [详细]
  • 《Axure新技能:自适应手机屏幕大小》相信不少人都已经看过,并对设置方法已经很熟悉了,但该教程只能适应iphone6的屏幕尺寸的比例&# ... [详细]
  • 在应用开发中少不了界面元素的布局、适配、自适应。之前苹果已经为我们提供了 AutoLayout 和 SizeClasses,方便我们实现页面的自适应弹性布局。但对于一些复杂的交互界 ... [详细]
  • 解决Cydia数据库错误:could not open file /var/lib/dpkg/status 的方法
    本文介绍了解决iOS系统中Cydia数据库错误的方法。通过使用苹果电脑上的Impactor工具和NewTerm软件,以及ifunbox工具和终端命令,可以解决该问题。具体步骤包括下载所需工具、连接手机到电脑、安装NewTerm、下载ifunbox并注册Dropbox账号、下载并解压lib.zip文件、将lib文件夹拖入Books文件夹中,并将lib文件夹拷贝到/var/目录下。以上方法适用于已经越狱且出现Cydia数据库错误的iPhone手机。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • 本文介绍了使用Python编写购物程序的实现步骤和代码示例。程序启动后,用户需要输入工资,并打印商品列表。用户可以根据商品编号选择购买商品,程序会检测余额是否充足,如果充足则直接扣款,否则提醒用户。用户可以随时退出程序,在退出时打印已购买商品的数量和余额。附带了完整的代码示例。 ... [详细]
  • macOS Big Sur全新设计大版本更新,10+个值得关注的新功能
    本文介绍了Apple发布的新一代操作系统macOS Big Sur,该系统采用全新的界面设计,包括图标、应用界面、程序坞和菜单栏等方面的变化。新系统还增加了通知中心、桌面小组件、强化的Safari浏览器以及隐私保护等多项功能。文章指出,macOS Big Sur的设计与iPadOS越来越接近,结合了去年iPadOS对鼠标的完善等功能。 ... [详细]
  • 本文介绍了互联网思维中的三个段子,涵盖了餐饮行业、淘品牌和创业企业的案例。通过这些案例,探讨了互联网思维的九大分类和十九条法则。其中包括雕爷牛腩餐厅的成功经验,三只松鼠淘品牌的包装策略以及一家创业企业的销售额增长情况。这些案例展示了互联网思维在不同领域的应用和成功之道。 ... [详细]
  • Unity3D引擎的体系结构和功能详解
    本文详细介绍了Unity3D引擎的体系结构和功能。Unity3D是一个屡获殊荣的工具,用于创建交互式3D应用程序。它由游戏引擎和编辑器组成,支持C#、Boo和JavaScript脚本编程。该引擎涵盖了声音、图形、物理和网络功能等主题。Unity编辑器具有多语言脚本编辑器和预制装配系统等特点。本文还介绍了Unity的许可证情况。Unity基本功能有限的免费,适用于PC、MAC和Web开发。其他平台或完整的功能集需要购买许可证。 ... [详细]
  • 本文介绍了iOS开发中检测和解决内存泄漏的方法,包括静态分析、使用instruments检查内存泄漏以及代码测试等。同时还介绍了最能挣钱的行业,包括互联网行业、娱乐行业、教育行业、智能行业和老年服务行业,并提供了选行业的技巧。 ... [详细]
  • 我一直都有记录信息的习惯,不知是从什么时候开始,大约是在工作后不久。如今还真有点庆幸从那时开始记了点东西,当然是电子版的,写 ... [详细]
  • Xcode离线安装帮助文档1.在线查看帮助文件:Xcode下查看帮助文件,菜单Help-DeveloperDocumentation在右上角搜索框中即可检索,但速度很慢 ... [详细]
  • 于2012年3月份开始接触OpenStack项目,刚开始之处主要是与同事合作共同部署公司内部的云平台,使得公司内部服务器能更好的得到资源利用。在部署的过程中遇到各种从未遇到过的问题 ... [详细]
author-avatar
fjkfjaslfjsal_577
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有