gpt4 book ai didi

iphone - iPhone 应用程序的 AirPrint

转载 作者:行者123 更新时间:2023-11-28 17:55:44 28 4
gpt4 key购买 nike

我正在尝试在我的应用程序中实现 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com