打印iPad屏幕代码

时间:2011-01-19 19:08:50

标签: xcode ipad printing airprint

您认为是否可以在应用程序(xcode)中添加代码,允许打印ipad的屏幕,或将其发送到Mac

2 个答案:

答案 0 :(得分:1)

#import "AirPrintingViewController.h"

@implementation AirPrintingViewController

-(void)printItem {

    NSString *path = [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"png"];
    NSData *dataFromPath = [NSData dataWithContentsOfFile:path];

    UIPrintInteractionController *printController = [UIPrintInteractionController sharedPrintController];

    if(printController && [UIPrintInteractionController canPrintData:dataFromPath]) {

        printController.delegate = self;

        UIPrintInfo *printInfo = [UIPrintInfo printInfo];
        printInfo.outputType = UIPrintInfoOutputGeneral;
        printInfo.jobName = [path lastPathComponent];
        printInfo.duplex = UIPrintInfoDuplexLongEdge;
        printController.printInfo = printInfo;
        printController.showsPageRange = YES;
        printController.printingItem = dataFromPath;

        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];

    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn addTarget:self action:@selector(printItem) forControlEvents:UIControlEventTouchDown];
    [btn setTitle:@"PRINT" forState:UIControlStateNormal];
    btn.frame = CGRectMake(0, 100, 320, 50);
    [self.view addSubview:btn];
}

@end

使用此代码我可以在路径中打印文件,如何打印我的屏幕而不是已经在路径中的文件?

答案 1 :(得分:0)

当然,但自己的应用。您无法发送其他应用的屏幕截图。