当我尝试打印当前屏幕截图(对于 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目录。