控制台错误:找不到PDF标题:找不到“%PDF”

时间:2012-04-08 07:53:54

标签: iphone objective-c ios xcode ipad

当我尝试打印当前屏幕截图(对于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)正确无误,并且已正确保存到文档目录。

2 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:0)

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

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

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