2011-06-08 2 views
1

J'essaie d'implémenter AirPrint dans mon application. À l'heure actuelle, mon code est le suivant:AirPrint pour iPhone App

AirPrintingViewController.h

#import <UIKit/UIKit.h> 

@interface AirPrintingViewController : UIViewController <UIPrintInteractionControllerDelegate>{ 

} 

-(void)printItem; 

@end 

AirPrintingViewController.m

#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 

Quand je lance l'application, j'appuie sur le bouton et il faut moi à une page d'impression. Lorsque j'active le simulateur d'imprimante et que j'appuie sur imprimer, rien ne se passe ...

J'ai essayé d'imprimer mon iPhone sur le simulateur d'imprimante et cela a fonctionné.

Est-ce que je fais quelque chose de mal?

Merci.

Répondre

-1

Êtes-vous sûr que l'image est trouvée? Essayez de consigner le chemin d'accès et dataFromPath.

NSLog(@"path = %@", path); 
NSLog(@"dataFromPath = %@", dataFromPath); 
+0

donnez une réponse correcte, ceci n'est pas une réponse – Ayaz