2012-07-11 56 views
0

任何人都可以告诉我如何使用UIPrintInteractionController从imageview打印图像。 谢谢从imageview打印图像:xcode

+4

生活(和学习如何编程)很大程度上是自助服务:http://www.google.com/search?q=UIPrintInteractionController – 2012-07-11 09:17:14

回答

1

首先读it.Follow链接使用:

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]; 
}