iOS 调用打印机
2016-02-02 08:40
936 查看
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上弹出打印那个页面
}
}
(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上弹出打印那个页面
}
}
相关文章推荐
- edgesForExtendedLayout ios7新特性
- iOS-线程&&进程的深入理解
- iOS知识小集 第一期
- iOS中常用的四种数据持久化方法简介
- 蓝懿IOS学习音频播放
- iOS开发的小技巧(断点打印)
- iOS 基础知识
- JS判断客户端是否是iOS或者Android
- ios开发技巧之【1】
- ios9系统图片显示不出来
- iOS开发:NSNotification的使用
- iOS—TableView之Cell的Style属性
- iOS—收起虚拟键盘的各种方法
- iOS深度学习 - Runtime
- iOS 的通知中心简介
- iOS 自动布局小结
- 查看某一个开发者代码修改量的脚本(ios平台可用)
- iOS开发 autoResizingMask使用
- 蜗牛爱课 -- iOS 设计模式之模板模式
- iOS zipzap读取压缩文件