2013-01-17 3 views
1

Salut, quelqu'un peut-il m'aider? Je suis nouveau sur le développement iOS. J'essaye de mettre en application une fonction d'impression, mais j'obtiens des erreurs dedans. Je ne vois rien, mais .xib fichier avec certains labels. Le textview est là, je veux juste imprimer ce point de vue ... quand j'appuyé sur le bouton d'impression du programme crashe ..Impression sur iOS

Voici mon code:

- (NSData *)generatePDFDataForPrinting { 
    NSMutableData *myPdfData = [NSMutableData data]; 
    UIGraphicsBeginPDFContextToData(myPdfData, kPDFPageBounds, nil); 
    UIGraphicsBeginPDFPage(); 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    [self drawStuffInContext:ctx]; // Method also usable from drawRect:. 
    UIGraphicsEndPDFContext(); 
    return myPdfData; 
} 

- (void)drawStuffInContext:(CGContextRef)ctx { 
    UIFont *font = [UIFont fontWithName:@"Zapfino" size:48]; 
    CGRect textRect = CGRectInset(kPDFPageBounds, 36, 36); 
    [@"hello world!" drawInRect:textRect withFont:font]; 
} 


- (IBAction)printFromIphone:(id)sender { 
    float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue]; 

    if (systemVersion>4.1) { 

     NSData *myPdfData = [NSData dataWithContentsOfFile:myPdfData]; //check the value inside |myPdfData| and |pdfPath| is the path of your pdf. 
     UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController]; 
     if (controller && [UIPrintInteractionController canPrintData:myPdfData]){ 
      //controller.delegate = delegate; //if necessary else nil 
      UIPrintInfo *printInfo = [UIPrintInfo printInfo]; 
      printInfo.outputType = UIPrintInfoOutputGeneral; 
      printInfo.jobName = [myPdfData lastPathComponent]; 
      //printInfo.duplex = UIPrintInfoDuplexLongEdge; 
      controller.printInfo = printInfo; 
      controller.showsPageRange = YES; 
      controller.printingItem = myPdfData; 

      // We need a completion handler block for printing. 
      UIPrintInteractionCompletionHandler completionHandler = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) { 
       if(completed && error){ 
        NSLog(@"FAILED! due to error in domain %@ with error code %u", error.domain, error.code); 
       } 
      }; 

      //   [controller presentFromRect:CGRectMake(200, 300, 100, 100) inView:senderView animated:YES completionHandler:completionHandler]; 
     }else { 
      NSLog(@"Couldn't get shared UIPrintInteractionController!"); 
     } 
    } 
} 
+0

Alors, quelle erreur obtenez-vous? –

+0

l'utilisation de l'identificateur non déclaré pdfData .... et pouvez-vous me suggérer un tutoriel exact ... plz..thanks –

Répondre

0

Je ne sais pas si une faute de frappe ou sa pas mais vous avez commenté votre pdfData réel qui est pourquoi il n'est pas déclaré.

Cette ligne doit être décommentée car vous avez besoin de myPdfData.

//NSData *myPdfData = [NSData dataWithContentsOfFile:pdfData]; //check the value inside |myPdfData| and |pdfPath| is the path of your pdf. 

Vous pouvez le remplacer par cette ligne à utiliser votre pdf au lieu d'un fichier.

NSData *myPdfData = [self generatePDFDataForPrinting]; 
+0

MERCI pour votre réponse et encore une erreur de plus dans la méthode ci-dessous identificateur non déclaré "rect" [controller presentFromRect: rect inView: senderView animé: YES completionHandler: completionHandler]; –

+0

Vous devez le présenter de quelque part. Le rect est juste l'endroit. Vous pouvez le remplir avec un CGRect d'où vous voulez qu'il apparaisse. –

Questions connexes