带走&从IOS打印截图

时间:2011-12-27 15:01:47

标签: iphone ios printing screenshot

我正试图截取屏幕并立即打印出来。由于某种原因,它没有正确回应...... 我将截图保存到文档目录中。这是代码:

-(IBAction)printdoc
{
    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();

    //UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
    NSData * imageData = UIImageJPEGRepresentation(viewImage, 1.0);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"Screenshot.jpg"];
    [imageData writeToFile:documentsDirectory atomically:YES];
    NSString *myFilePath = [documentsDirectory stringByAppendingPathComponent:@"Screenshot.jpg"];
    NSData *myData = [NSData dataWithContentsOfFile:myFilePath];

    UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
    if  (pic && [UIPrintInteractionController canPrintData:myData] ) {
        pic.delegate = self;
        NSLog(@"CAN PRINT !!!!!!!!!!!!");
        UIPrintInfo *printInfo = [UIPrintInfo printInfo];
        printInfo.outputType = UIPrintInfoOutputGeneral;
        printInfo.jobName = [myFilePath lastPathComponent];
        printInfo.duplex = UIPrintInfoDuplexLongEdge;
        pic.printInfo = printInfo;
        pic.showsPageRange = YES;
        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);
        };

        // iPad only printing

        [pic presentAnimated:YES completionHandler:completionHandler];
    }
}

1 个答案:

答案 0 :(得分:2)

稍微更新一下!解决了 ! 实际上我的代码工作了,这是我的打印机做的问题...现在使用上面的代码很好!现在我想知道如何为iPad设置它