2011-12-27 9 views
2

私はスクリーンショットを撮ってすぐに印刷しようとしています。何らかの理由でそれが正しく応答しません... 私はドキュメントディレクトリにスクリーンショットを保存しています。コードは次のとおりです。IOSのスクリーンショットを取得してプリントします

-(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]; 
    } 
} 
+0

私はタイトルでXcodeという単語を取り除きました。実際の問題とはほとんど関係ないでしょう。あなたの質問で、ロイ。 –

+0

ああ、申し訳ありません:) –

+0

もちろんXcodeタグも削除する必要があります。 ;-) –

答えて

2

少し更新!解決済み! 実際には私のコードがうまくいきました。問題を起こすのは私のプリンタでした...上のコードでうまくいきました!今iPad向けに設定する方法を理解したい

+0

上記のコードがiPADで正しく動作するようになったのですか?可能であれば、完全な作業コードを提供してください。 – JAHelia

関連する問題