airprint - UIPrintInteractionController打印问题

标签 airprint

我向自定义 QLPreviewController 的 navigationItem 添加了一个操作按钮。当点击操作按钮时,我会呈现一个 UIPrintInteractionController。 我正在从我的应用程序的文档目录中获取文件。预览时没有问题。但是,当我通过点击操作按钮打印同一文件时,[UIPrintInteractionController canPrintData:data] 返回 false。但如果该文件位于我的应用程序根目录内,它就可以工作。

下面是点击操作按钮时执行的代码。

- (void)tappedPrintButton:(id) sender { 
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];

NSURL *fileURL = (NSURL *)[self currentPreviewItem];

NSData *data = [NSData dataWithContentsOfURL:fileURL];

if  (pic && [UIPrintInteractionController canPrintData:data] ) {

    pic.delegate = self;



    UIPrintInfo *printInfo = [UIPrintInfo printInfo];

    printInfo.outputType = UIPrintInfoOutputGeneral;

    printInfo.jobName = [(NSURL *)[self.files objectAtIndex:0] lastPathComponent];

    printInfo.duplex = UIPrintInfoDuplexLongEdge;

    pic.printInfo = printInfo;

    pic.showsPageRange = YES;

    pic.printingItem = data;



    void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =

    ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {

        if (!completed && error)

            NSLog(@"FAILED! due to error in domain %@ with error code %u",

                  error.domain, error.code);

    };

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {

        [pic presentFromBarButtonItem:self.myActionBarButton animated:YES

                    completionHandler:completionHandler];

    } else {

        [pic presentAnimated:YES completionHandler:completionHandler];

    }
}
}

我无法想象这个问题。请帮忙...

最佳答案

UIPrintInteractionControllerprintingItem 属性是 documented仅支持 PDF 和图像数据:

The object must be an instance of the NSURL, NSData, UIImage, or ALAsset class. An object of the first two types must reference or contain image data or PDF data.

如果您想使用 UIPrintInteractionController 打印非 PDF、非图像数据(例如 Office 文档),则必须使用 printFormatter 属性。

您可以通过打印格式化程序打印 UIWebViewUITextViewMKMapView 内容,无需任何自定义逻辑。这记录在 UIViewPrintFormatter documentation 中:

An instance of the UIViewPrintFormatter class lays out the drawn content of a view for printing. The view’s content can span multiple pages.

Instances of three system classes offer usable view print formatters to applications: UIWebView and UITextView of the UIKit framework, and MKMapView of the Map Kit framework. To obtain a view print formatter for a print job, call the UIView method viewPrintFormatter and initialize the print formatter’s inherited layout properties.

不幸的是,QLPreviewController 的 View 没有记录为返回有效的viewPrintFormatter。这意味着您将无法使用 QLPreviewController 滚动自己的自定义打印代码。您可以考虑使用 UIWebView 来代替它来呈现文档。

关于airprint - UIPrintInteractionController打印问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10854492/

相关文章:

ios - 在 XCode 6 beta 7 中从 Swift 在 iOS 中打印

ios - IPP 通过 HTTP : getting 400 bad request as a response

iphone - 在ios中使用打印机打印nsmutable数组

ios - UIPrintInteractionController 不显示纸张选择

ios - 使用 Swift 在 iOS 中打印 View

ios - 使用 AirPrint 在连续纸卷上打印时出现不需要的额外空白空间

swift - AirPrint (UIPrintInteractionController) 使用 iOS 9、swift 2、XCode 7 打印警告

pdf - iOS7 更新 - 在 UIWebView 中加载的 Airprinting PDF 产生缩放 PDF 到字母页面大小的 50%

iphone - 使用 UIPrintInteractionController 打印 PDF ios 5 有效,但在 ios 6 中不起作用并使我的应用程序崩溃