UIPrintInteractionController canPrintUrl始终为false,错误CFURLCreateDataAndPropertiesFromResource:失败,错误代码为-15

时间:2014-10-21 07:03:53

标签: ios objective-c iphone

我遇到UIPrintInteractionController的问题,canPrintUrl总是返回false和错误代码 :CFURLCreateDataAndPropertiesFromResource:失败,错误代码为-15。 :ImageIO:CGImageSourceCreateWithURL CFURLCreateDataAndPropertiesFromResource失败,错误代码为-15。

打印网址说明: 的 /var/mobile/Applications/49C03664-6108-40D2-94A3-C545F0234ACE/Documents/mypdf.pdf

它是一个有效的PDF并在adobe reader和UIWebView中打开。

-(void)printButtonTapped:(CGRect)rect
{
    if ([UIPrintInteractionController isPrintingAvailable] == YES)
    {
        // NSURL *fileURL = document.fileURL; // Document file URL
        NSString* str=[NSString stringWithFormat:@"%@/Documents/mypdf.pdf",NSHomeDirectory()];
        NSURL* url=[NSURL URLWithString:str];



        if ([UIPrintInteractionController canPrintURL:url] == YES)
        {
            self.printInteraction = [UIPrintInteractionController sharedPrintController];

            UIPrintInfo *printInfo = [UIPrintInfo printInfo];
            printInfo.duplex = UIPrintInfoDuplexLongEdge;
            printInfo.outputType = UIPrintInfoOutputGeneral;
            printInfo.jobName = [url lastPathComponent];;

            self.printInteraction.printInfo = printInfo;
            self.printInteraction.printingItem = url;
            self.printInteraction.showsPageRange = YES;

            if ([[UIDevice currentDevice]userInterfaceIdiom] == UIUserInterfaceIdiomPad) // Large device printing
            {
                [self.printInteraction presentFromRect:rect inView:self.view animated:YES completionHandler:
                 ^(UIPrintInteractionController *pic, BOOL completed, NSError *error)
                 {
#ifdef DEBUG
                     if ((completed == NO) && (error != nil)) NSLog(@"%s %@", __FUNCTION__, error);
#endif
                 }
                 ];
            }
            else // Handle printing on small device
            {
                [self.printInteraction presentAnimated:YES completionHandler:
                 ^(UIPrintInteractionController *pic, BOOL completed, NSError *error)
                 {
#ifdef DEBUG
                     if ((completed == NO) && (error != nil)) NSLog(@"%s %@", __FUNCTION__, error);
#endif
                 }
                 ];
            }
        }
    }
}

我在WevView中加载上面的url,然后显示PDF。 如果我将pdf加载的webview.request.url传递给[UIPrintInteractionController canPrintURL:webview.request.URL] 然后正常工作并打印pdf。

我不知道发生了什么错误我在ios7和ios8上运行。

有谁可以帮我解决这个问题?

3 个答案:

答案 0 :(得分:0)

在指定位置找不到文件或文件名包含空格。 webview期望文件URL中的编码空格,而UIPrintInteractionController期望文件URL中有正常的空格。修复应解决问题。

答案 1 :(得分:0)

您必须知道canPrintDatacanPrintURL之间存在差异:

<强>代码

NSString* str=[NSString stringWithFormat:@"%@/Documents/mypdf.pdf",NSHomeDirectory()];
NSURL* url=[NSURL URLWithString:str];
if ([UIPrintInteractionController canPrintURL:url] == YES)
....

不对。对于本地文件,您应使用:

NSString* str=[NSString stringWithFormat:@"%@/Documents/mypdf.pdf",NSHomeDirectory()];
NSData *data = [NSData dataWithContentsOfFile:str];
if ([UIPrintInteractionController canPrintData:yourfilepath] == YES)

答案 2 :(得分:0)

使用 UIPrintInteractionController.printableUTIs 检查您的文件类型(.pdf,.docx,.txt)是否支持打印。