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

[date writeToFile:appstr アトミック:YES];

CURRENTVIEW.PNGのデータをプリンターに渡す

于 2013-11-09T14:27:30.057 に答える
0

UIPrintInteraction を統合するには、UIPrintInteractionControllerDelegate を追加し、以下のコードを使用します

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 に答える