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
原创粉丝点击