iphone - iPhone 应用程序的 AirPrint

标签 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 将 airprint 打印到打印机模拟器并且成功了。

我做错了什么吗?

谢谢。

最佳答案

你确定图片找到了吗?

尝试记录路径和 dataFromPath。

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

关于iphone - iPhone 应用程序的 AirPrint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6277875/

相关文章:

iphone - 没有音频处理的 AVCaptureSession 视频预览

javascript - 将 JavaScript 函数注入(inject) UIWebView 并不总是有效

objective-c - 由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序,原因 : '-[NSCFString objectForKey:]: unrecognized selector sent to instance

ios - 我如何减少解析 sdk 集成 ios 上的应用程序大小

java - 使用收据打印机打印收据

python - Jupyter 笔记本 : Print Pandas Dataframe without wrapping on a new line (print statement is in external function)

iphone - 在没有 Apple Developer 帐户的情况下在 iPhone 上测试 react-native 应用程序

iphone - uitextfield隐藏键盘?

ios - xcode exc_bad_access : Please teach how to properly debug this error

javascript - 打印到纸张的特定区域 C#