AirPrint for iPhone App

时间:2011-06-08 11:14:54

标签: iphone ios printing uiview airprint

我正在尝试将AirPrint应用到我的应用程序中。目前,我的代码是这样的:

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

当我运行应用程序时,我按下按钮,它将我带到打印页面。当我启用打印机模拟器然后按打印时,没有任何反应......

我试图通过我的iPhone进行打印到打印机模拟器,它工作正常。

我做错了吗?

感谢。

1 个答案:

答案 0 :(得分:-1)

你确定找到了照片吗?

尝试记录路径和dataFromPath。

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