任何人都可以告诉我如何使用UIPrintInteractionController从imageview打印图像。 谢谢
答案 0 :(得分:1)
首先阅读AirPrinter以及如何使用它。关注链接: https://github.com/kharrison/CodeExamples/blob/master/AirPrinter/AirPrinter
您需要添加文件 UYLGenericPrintPageRenderer.h 和 UYLGenericPrintPageRenderer.m
从这里获取:
这些是PrintPageRenderer文件。
在您的班级中导入此文件:
#import "UYLGenericPrintPageRenderer.h"
现在检查您的设备是否支持打印:
if ([UIPrintInteractionController isPrintingAvailable]) {
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(printImageView)];
[self.navigationItem setRightBarButtonItem:barButton animated:NO];
self.printButton = barButton;
[barButton release];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Sorry, Printing is not available" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
[alert release];
}
现在打印此处使用的“printImageView”图像作为UIImageView类的对象。
- (void)printImageView{
UIPrintInteractionController *pc = [UIPrintInteractionController sharedPrintController];
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = @"Sample Print";
pc.printInfo = printInfo;
pc.showsPageRange = YES;
UYLGenericPrintPageRenderer *renderer = [[UYLGenericPrintPageRenderer alloc] init];
renderer.headerText = printInfo.jobName;
renderer.footerText = @"AirPrinter Sample";
UIViewPrintFormatter *formatter = [printImageView viewPrintFormatter];
[renderer addPrintFormatter:formatter startingAtPageAtIndex:0];
pc.printPageRenderer = renderer;
[renderer release];
UIPrintInteractionCompletionHandler completionHandler =
^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
if(!completed && error){
DLog(@"Print failed - domain: %@ error code %u", error.domain, error.code);
}
};
[pc presentFromBarButtonItem:self.printButton animated:YES completionHandler:completionHandler];
}
答案 1 :(得分:-1)
将图片转换为pdf并打印pdf。