我向自定义 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];
}
}
}
我无法想象这个问题。请帮忙...
最佳答案
UIPrintInteractionController
的 printingItem
属性是 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
属性。
您可以通过打印格式化程序打印 UIWebView
、UITextView
和 MKMapView
内容,无需任何自定义逻辑。这记录在 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/