iOS - 获取警告但无法打印

时间:2012-01-02 09:21:31

标签: ios printing

我正在使用iOS 5.0.1开发应用。 我正在使用以下代码在我的ipad应用程序中打印视图的屏幕截图:

CGRect rect = CGRectMake(0, 0, 768, 1004);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.view.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();       
UIGraphicsEndImageContext();

UIPrintInteractionController* pic = [UIPrintInteractionController sharedPrintController];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(img)];
if (pic && [UIPrintInteractionController canPrintData:imageData])
    {
        pic.delegate = self;
        UIPrintInfo* printInfo = [UIPrintInfo printInfo];
        printInfo.outputType = UIPrintInfoOutputPhoto;
        printInfo.jobName = @"PrintingImage";
        printInfo.duplex = UIPrintInfoDuplexLongEdge;
        pic.printInfo = printInfo;
        pic.showsPageRange = YES;
        pic.printingItem = imageData;

        void (^completionHandler) (UIPrintInteractionController *, BOOL, NSError*) = ^(UIPrintInteractionController *pic, BOOL completed, NSError* error) {
            if (!completed && error) {
                NSLog(@"Error: %@", error.localizedDescription);
            }  
        };

        [pic presentAnimated:YES completionHandler:completionHandler];
    }

它没有显示打印视图并抛出以下错误:

WARNING: Calling -[UIPrintInteractionController presentAnimated:completionHandler:] on iPad
failed to find PDF header: `%PDF' not found.
failed to find PDF header: `%PDF' not found.
failed to find PDF header: `%PDF' not found.
failed to find PDF header: `%PDF' not found.

1 个答案:

答案 0 :(得分:8)

我通过使用以下与ipad

相关的方法解决了这个问题
- (BOOL)presentFromRect:(CGRect)rect inView:(UIView *)view animated:(BOOL)animated completionHandler:(UIPrintInteractionCompletionHandler)completion;    // iPad
- (BOOL)presentFromBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated completionHandler:(UIPrintInteractionCompletionHandler)completion;      // iPad