ios调用打印机
来源:互联网 发布:python中字典的用法 编辑:程序博客网 时间:2024/06/04 17:50
1)添加UIPrintInteractionControllerDelegate 打印的委托代理协议
(2)添加打印按钮:
[objc]
// 打印
UIButton *printButton = [UIButton buttonWithType:UIButtonTypeSystem];
printButton.frame =CGRectMake(400,0,100,70);
[printButton setTitle:@"打印" forState:UIControlStateNormal];
printButton.titleLabel.font = [UIFont systemFontOfSize:32];
[printButton addTarget:self action:@selector(printAction:) forControlEvents:UIControlEventTouchUpInside];
[self.toolView addSubview:printButton];
(3)打印button相应的action
打印一张图片:
[objc]
// 打印
-(void)printAction:(id)sender{
UIPrintInteractionController *printC = [UIPrintInteractionController sharedPrintController];//显示出打印的用户界面。
printC.delegate = self;
UIImage *img = [UIImage imageNamed:@"db.png"];
NSData *data = [NSData dataWithData:UIImagePNGRepresentation(img)];
if (printC && [UIPrintInteractionController canPrintData:data]) {
UIPrintInfo *printInfo = [UIPrintInfo printInfo];//准备打印信息以预设值初始化的对象。
printInfo.outputType = UIPrintInfoOutputGeneral;//设置输出类型。
printC.showsPageRange = YES;//显示的页面范围
// printInfo.jobName = @"willingseal";
// printC.printInfo = printInfo;
// NSLog(@"printinfo-%@",printC.printInfo);
printC.printingItem = data;//single NSData, NSURL, UIImage, ALAsset
// NSLog(@"printingitem-%@",printC);
// 等待完成
void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
if (!completed && error) {
NSLog(@"可能无法完成,因为印刷错误: %@", error);
}
};
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:sender];//调用方法的时候,要注意参数的类型-下面presentFromBarButtonItem:的参数类型是 UIBarButtonItem..如果你是在系统的UIToolbar or UINavigationItem上放的一个打印button,就不需要转换了。
[printC presentFromBarButtonItem:item animated:YES completionHandler:completionHandler];//在ipad上弹出打印那个页面
} else {
[printC presentAnimated:YES completionHandler:completionHandler];//在iPhone上弹出打印那个页面
}
}
}
打印当前网页或者文本信息:
[objc]
@property (strong,nonatomic)UIWebView *webView;
@property (strong,nonatomic)UIView *toolView;
@property (strong,nonatomic)NSString *currentURL;
[objc]
-(void) webViewDidFinishLoad:(UIWebView *)webView {
self.currentURL = webView.request.URL.absoluteString;//获取当前网页的url
NSLog(@"--url-%@--",self.currentURL);
}
[objc]
// 打印
-(void)printAction:(id)sender{
UIPrintInteractionController *printC = [UIPrintInteractionController sharedPrintController];//显示出打印的用户界面。
printC.delegate = self;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];//准备打印信息以预设值初始化的对象。
printInfo.outputType = UIPrintInfoOutputGeneral;//设置输出类型。
printC.showsPageRange = YES;//显示的页面范围
// 打印网页
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.currentURL]]];//网页
printC.printFormatter = [self.webView viewPrintFormatter];//布局打印视图绘制的内容。
/*
// 打印文本
UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc]
initWithText:@"ここの ういえい 子に うぃっl willingseal 20655322 你好么? #@¥%……&*"];
textFormatter.startPage = 0;
textFormatter.contentInsets = UIEdgeInsetsMake(200, 300, 0, 72.0); // 插入内容页的边缘 1 inch margins
textFormatter.maximumContentWidth = 16 * 72.0;//最大范围的宽
printC.printFormatter = textFormatter;
*/
// 等待完成
void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
if (!completed && error) {
NSLog(@"可能无法完成,因为印刷错误: %@", error);
}
};
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:sender];//调用方法的时候,要注意参数的类型-下面presentFromBarButtonItem:的参数类型是 UIBarButtonItem..如果你是在系统的UIToolbar or UINavigationItem上放的一个打印button,就不需要转换了。
[printC presentFromBarButtonItem:item animated:YES completionHandler:completionHandler];//在ipad上弹出打印那个页面
// [printC presentFromRect:CGRectMake(500, 500, 100, 200) inView:self.webView animated:YES completionHandler:completionHandler];//第二种方法
} else {
[printC presentAnimated:YES completionHandler:completionHandler];//在iPhone上弹出打印那个页面
}
}
0 0
- iOS 调用打印机
- Ios 调用打印机2
- ios调用打印机
- ios 打印机
- VB调用打印机属性
- 调用打印机,js
- WebService 调用打印机
- C# 调用打印机
- js调用打印机打印
- java 调用打印机程序
- Qt调用打印机
- js调用打印机服务
- 调用windows打印机
- Qt 调用打印机参考
- jQuery调用打印机
- AX代码调用打印机
- c# 调用打印机
- java调用打印机
- UINavigationBar
- 程序员面试题精选100题(24)-栈的push、pop序列[数据结构]
- Cannot forward after response has been committed
- Vs2010中的属性配置文件.
- 通用excel数据导出/转换(三)
- ios调用打印机
- Vim命令笔记
- 最简单的css和js文件合并
- 1.教你在mac上怎么用jersey搭restful服务
- 程序员面试题精选100题(25)-在从1到n的正数中1出现的次数[算法]
- iOS开发之指纹识别
- 一次正则表达式的疑惑
- JavaBean与表单
- scrollView滚动到底部