0

谁能告诉我如何使用 UIPrintInteractionController 从 imageview 打印图像。谢谢

4

2 回答 2

1

首先阅读 AirPrinter 以及如何使用它。点击链接: https ://github.com/kharrison/CodeExamples/blob/master/AirPrinter/AirPrinter

您需要添加文件UYLGenericPrintPageRenderer.hUYLGenericPrintPageRenderer.m

从这里获取:

https://github.com/kharrison/CodeExamples/blob/master/AirPrinter/AirPrinter/UYLGenericPrintPageRenderer.h

https://github.com/kharrison/CodeExamples/blob/master/AirPrinter/AirPrinter/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];
}
于 2012-07-11T10:17:28.520 回答
-1

将图像转换为 pdf 并打印 pdf。

于 2012-07-11T09:33:34.207 回答