iPhone:打印滚动视图的所有内容

时间:2012-02-27 23:06:56

标签: iphone ios xcode printing uiscrollview

我需要你的帮助。我已经阅读了有关打印的Apple文档,但是我很难理解如何打印Scroll View的所有内容。到目前为止,我知道检查打印机是否可用

//-----------Check for printer-------------------
if ([UIPrintInteractionController isPrintingAvailable]) {
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
                                                                               target:self 
                                                                               action:@selector(printWebView:)];

    [self.navigationItem setLeftBarButtonItem:barButton animated:NO];
    self.printButton = barButton;
}

如果有人可以提供实际打印作业的代码,我将非常感激。 提前谢谢......

2 个答案:

答案 0 :(得分:2)

抓取截图并打印

/ *抓取屏幕截图并打印* /

-(void)printItem {

UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();


UIPrintInteractionController *printController = [UIPrintInteractionController sharedPrintController];

if(printController ) {

    printController.delegate = self;

    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    printInfo.outputType = UIPrintInfoOutputGeneral;
    printInfo.jobName = @"resultado";
    printInfo.duplex = UIPrintInfoDuplexLongEdge;
    printController.printInfo = printInfo;
    printController.showsPageRange = YES;
    printController.printingItem = newImage;

    void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
        if (!completed && error) {
            NSLog(@"FAILED! due to error in domain %@ with error code %u", error.domain, error.code);
        }
    };

    [printController presentAnimated:YES completionHandler:completionHandler];

}
}

答案 1 :(得分:0)

您是否考虑过创建UIImage(屏幕截图,排序)并打印生成的图像?我不知道打印,但我可以帮助截图。请注意,如果您的滚动视图不适合屏幕,则需要修改以下代码以获得更大的界限。

享受,

达明

- (UIImage*)screenshot {
// Create a graphics context with the target size
// On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
// On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
CGSize imageSize = [[UIScreen mainScreen] bounds].size;
if (NULL != UIGraphicsBeginImageContextWithOptions)
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
else
    UIGraphicsBeginImageContext(imageSize);

CGContextRef context = UIGraphicsGetCurrentContext();

// Iterate over every window from back to front
for (UIWindow *window in [[UIApplication sharedApplication] windows]) 
{
    if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen])
    {
        // -renderInContext: renders in the coordinate space of the layer,
        // so we must first apply the layer's geometry to the graphics context
        CGContextSaveGState(context);
        // Center the context around the window's anchor point
        CGContextTranslateCTM(context, [window center].x, [window center].y);
        // Apply the window's transform about the anchor point
        CGContextConcatCTM(context, [window transform]);
        // Offset by the portion of the bounds left of and above the anchor point
        CGContextTranslateCTM(context,
                              -[window bounds].size.width * [[window layer] anchorPoint].x,
                              -[window bounds].size.height * [[window layer] anchorPoint].y);

        // Render the layer hierarchy to the current context
        [[window layer] renderInContext:context];

        // Restore the context
        CGContextRestoreGState(context);
    }
}

// Retrieve the screenshot image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return image;
}

注意:我从教程中获得了此代码。它有效(我在发货的代码中有它),但我不了解每一行。