iOS 共享到打印机UIPrintInteractionController

iOS 共享爬坑记

瞅了一圈,估计也就是打印机最简单一些,那就先来打印机吧。只是没想到,在领导的感觉,打印机反倒成了最难搞的一个了,实在是……哎。

话不多说,直接上代码。

- (void)printAction {
    UIPrintInteractionController *print = [UIPrintInteractionController sharedPrintController];
    NSMutableArray *tempMarr = [NSMutableArray arrayWithCapacity:0];
    NSData *pdfData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"4" ofType:@"pdf"]];
    if (!pdfData) {
        NSLog(@"file not exist");
        return;
    }
    [tempMarr addObject:pdfData];

    // ⚠️ 打印图片,与PDF不能同时打印

    if (print) {
        UIPrintInfo *printInfo = [UIPrintInfo printInfo];
        // 打印输出类型
//        printInfo.outputType = UIPrintInfoOutputPhoto;
        printInfo.outputType = UIPrintInfoOutputGeneral;
        // 默认应用程序名称
        printInfo.jobName = @"PDF";
        // 双面打印信息,NONE为禁止双面
        printInfo.duplex = UIPrintInfoDuplexLongEdge;
        // 打印纵向还是横向
        printInfo.orientation = UIPrintInfoOrientationPortrait;
        print.printInfo = printInfo;

        print.delegate = self;
//        print.showsPageRange = YES;
        print.printingItems = tempMarr;

        void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
            if (completed) {
                NSLog(@"打印完成");
            }else {
                NSLog(@"打印取消");
            }
            if (error) {
                NSLog(@"打印出错");
            }
        };
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
            [print presentFromRect:self.view.frame inView:self.view animated:YES completionHandler:completionHandler];
        } else {
            self.modalPresentationStyle = UIModalPresentationFullScreen;
            [print presentAnimated:YES completionHandler:completionHandler];
        }
    }
}

就这么简单?没错,就这么简单!

你可能感兴趣的:(iOS 共享到打印机UIPrintInteractionController)