首页 > 代码库 > iOS UIPrintInteractionController打印

iOS UIPrintInteractionController打印

- (void)printData

{

    //为打印做准备,创建一个指向sharedPrintController的引用

    UIPrintInteractionController *printer = [UIPrintInteractionController sharedPrintController];

    printer.delegate = self;

    

    //配置打印信息

    UIPrintInfo *Pinfo = [UIPrintInfo printInfo];

    Pinfo.outputType = UIPrintInfoOutputGeneral;//可打印文本、图形、图像

    Pinfo.jobName = @"Print for xiaodui";//可选属性,用于在打印中心中标识打印作业

    Pinfo.duplex = UIPrintInfoDuplexLongEdge;//双面打印绕长边翻页,NONE为禁止双面

    Pinfo.orientation = UIPrintInfoOrientationPortrait;//打印纵向还是横向

    //    Pinfo.printerID = @"";//指定默认打印机,也可以使用UIPrintInteractionControllerDelegate来知悉

    printer.printInfo = Pinfo;

    

    //设置页面范围

//    UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc] initWithText:@"哈护手霜按时哈哈"];

//    textFormatter.startPage = 0;//指定从哪一张开始打印0代表第一张

//    textFormatter.contentInsets = UIEdgeInsetsMake(36, 36, 36, 36);//72相当于1英寸,这样设置上下左右的边距都为0.5英寸

//    textFormatter.maximumContentWidth = 504;//(72x7.5)相当于打印宽度为7英寸

//    printer.printFormatter = textFormatter;

    

//    printer.printingItem = [UIImage imageNamed:@"LaunchImage"];

    printer.printingItems = @[[UIImage imageNamed:@"welcome_page2"], [UIImage imageNamed:@"LaunchImage"], [UIImage imageNamed:@"welcome_page1"],];

    

    printer.showsPageRange = NO;

    

    [printer presentAnimated:YES completionHandler:^(UIPrintInteractionController * _Nonnull printInteractionController, BOOL completed, NSError * _Nullable error) {

        if (!completed && error) {

            NSLog(@"Error");

        }

    }];

}

 

iOS UIPrintInteractionController打印