2012-04-08 364 views
4

当我尝试打印当前屏幕截图(对于iPad视图)时,我在控制台窗口中看到这个奇怪的错误: failed to find PDF header: %PDF not found控制台错误:无法找到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)是正确的,它是正确保存到文档目录。

回答

2

这将有助于确切知道哪一行导致该控制台输出。你可以在调试器中找到它。

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

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

+0

这是触发该错误的行:[self.pic presentFromRect:self.printButton.frame inView:self.view animated:YES completionHandler:completionHandler]; – JAHelia 2012-04-08 08:13:27

+0

猜猜我猜错了!但是OS X和iOS打印系统通常基于PDF。也许印刷管道中的其他事情正在做类似的检查。 – 2012-04-08 08:18:09

+0

那么,这个错误会在打印过程中引起问题吗?我无法测试打印过程,'因为我没有支持AirPlay的打印机。 – JAHelia 2012-04-08 08:24:51

0

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

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

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