【问题标题】:AirPrint for iPhone AppiPhone 版 AirPrint 应用程序
【发布时间】:2011-06-08 11:14:53
【问题描述】:

我正在尝试在我的应用程序中实现 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 向打印机模拟器进行空气打印,并且成功了。

我做错了吗?

谢谢。

【问题讨论】:

    标签: iphone ios printing uiview airprint


    【解决方案1】:

    你确定找到图片了吗?

    尝试记录路径和 dataFromPath。

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

    【讨论】:

    • 给出正确答案,这不是答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-11
    • 2011-01-01
    相关资源
    最近更新 更多