iphone – 控制台错误:找不到PDF标题:找不到`%PDF’

当我尝试打印当前屏幕截图(对于iPad视图)时,我在控制台窗口中遇到这个奇怪的错误:

找不到PDF标题:找不到%PDF.

我想知道为什么在控制台窗口中触发此错误?为什么它提到了与PDF相关的东西,虽然我没有在整个项目中使用任何PDF相关的东西.

用于打印屏幕截图的代码是:

  - (IBAction)print:(id)sender {

    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
        UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
     else
         UIGraphicsBeginImageContext(self.view.bounds.size);

     [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

     UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

     UIGraphicsEndImageContext();


    //saving the file to documents directory

     NSData * imageData = UIImagePNGRepresentation(viewImage);
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *documentsDirectory = [paths objectAtIndex:0];
     documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"Screenshot.png"];
     [imageData writeToFile:documentsDirectory atomically:YES];

     NSString *myFilePath = [documentsDirectory stringByAppendingPathComponent:@"Screenshot.png"];
    UIImage *screenShot = [UIImage imageWithData:imageData];
    NSData *myData = [NSData dataWithData:UIImagePNGRepresentation(screenShot)];

     self.pic = [UIPrintInteractionController sharedPrintController];
      if (self.pic && [UIPrintInteractionController canPrintData:myData] ) {

         self.pic.delegate = self;

        //filling up print info
         UIPrintInfo *printInfo = [UIPrintInfo printInfo];
         printInfo.outputType = UIPrintInfoOutputGeneral;
         printInfo.jobName = [myFilePath lastPathComponent];
         printInfo.duplex = UIPrintInfoDuplexLongEdge;
         self.pic.printInfo = printInfo;

         self.pic.showsPageRange = YES;
         self.pic.printingItem = myData;

        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);
             };

        [self.pic presentFromRect:self.printButton.frame inView:self.view animated:YES completionHandler: completionHandler];    // iPad


        }

    }

文件路径(Screenshot.png)是正确的,并且它已正确保存到文档目录.

最佳答案 这将有助于知道哪条线确切地导致控制台输出.您可以在调试器中单步执行以查找.

我的猜测是调用 – [UIPrintInteractionController canPrintData:].如果您阅读the documentation for that method,您将看到它必须检查该数据是否包含PDF.它看起来像尝试和失败,沿途打印一条消息.

由于一般没有最终用户观看iOS控制台,这不是一个非常重要的错误,显然对Apple来说不够重要.

点赞