4

当我尝试打印当前屏幕截图(对于 iPad 视图)时,我在控制台窗口中收到了这个奇怪的错误: failed to find PDF header: %PDF not found.

我想知道为什么在控制台窗口中会触发此错误?为什么它提到了一些相关的东西,尽管我在整个项目中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)正确,正确保存到documents目录。

4

2 回答 2

2

确切地知道哪一行导致该控制台输出会有所帮助。您可以在调试器中单步执行以找出答案。

我的猜测是,这是对-[UIPrintInteractionController canPrintData:]. 如果您阅读该方法的文档,您会发现它必须检查该数据是否包含 PDF。看起来它尝试并失败,沿途打印一条消息。

由于通常没有最终用户观看 iOS 控制台,因此这不是一个非常重要的错误,显然不足以让 Apple 修复。

于 2012-04-08T08:01:27.110 回答
0

这也发生在我身上。我检查了每一行,发现这是因为您正在打印数据。如果你直接打印图像,这个奇怪的警告就会消失。因为来自 Apple 的 UIPrintInteractionController 框架文件:

@NSCopying public var printingItem: AnyObject? // single NSData, NSURL, UIImage, ALAsset

printItem 可以是上述四种类型中的任何一种。我的猜测是 Apple 默认将数据作为 PDF 数据,显然,这不是您从 UIImage 生成的数据中的标题数据。

于 2016-06-08T22:07:07.993 回答