0

我正在使用 xcode 5。我想知道如何空中打印当前视图。所以一个简单的按钮,上面写着“打印”,然后当用户点击它时,它会在屏幕上打印所有内容。我将如何实现这一点?

4

2 回答 2

2

截取视图

并在路径中保存为 png 并将数据传递给 UIPrintInteractionController

截屏

UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

CGRect rect=self.view.frame;


CGImageRef cropped1 = CGImageCreateWithImageInRect(viewImage.CGImage, rect);
UIImage *imges =  [UIImage imageWithCGImage:cropped1]; 
CGImageRelease(cropped1);
// squarecropimg.image=imges;

NSData *date=UIImagePNGRepresentation(imges);
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *doc=[paths objectAtIndex:0];
NSString *appstr=[doc stringByAppendingPathComponent:@“currentview.png"];

[日期 writeToFile:appstr atomically:YES];

将 CURRENTVIEW.PNG 的数据传递给打印机

于 2013-11-09T14:27:30.057 回答
0

FOR integrating UIPrintInteraction add UIPrintInteractionControllerDelegate and use the below code

UIPrintInteractionController *print_picker = [UIPrintInteractionController sharedPrintController];

if(print_picker && [UIPrintInteractionController canPrintData: **CURRENTIMAGE DATA HERE** ] ) {

    print_picker.delegate = self;

    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    printInfo.outputType = UIPrintInfoOutputGeneral;
    printInfo.jobName = @"JOBNAME";
    printInfo.duplex = UIPrintInfoDuplexLongEdge;
    print_picker.printInfo = printInfo;
    print_picker.showsPageRange = YES;
    print_picker.printingItem = **CURRENTIMAGE DATA HERE**;

    void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
        //self.content = nil;
        if (!completed && error) {
            NSLog(@"FAILED! due to error in domain %@ with error code %u", error.domain, error.code);
        } 
    };

    [print_picker presentAnimated:YES completionHandler:completionHandler];

} 
于 2013-11-09T15:05:28.233 回答