1

我正在尝试在我的应用程序中实现 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 打印到打印机模拟器并且它工作正常。

难道我做错了什么?

谢谢。

4

1 回答 1

-1

你确定图片找到了?

尝试记录路径和 dataFromPath。

NSLog(@"path = %@", path);
NSLog(@"dataFromPath = %@", dataFromPath);
于 2012-01-16T09:15:23.907 回答