1

こんにちは、これで私を助けてくれる人はいますか?iOS開発初心者です。印刷機能を実装しようとしていますが、エラーが発生します。.xibいくつかのファイルしか表示されませんlabels。そこtextviewにあります。そのビューを印刷したいだけです...印刷ボタンを押すと、プログラムがクラッシュします..

これが私のコードです:

- (NSData *)generatePDFDataForPrinting {
    NSMutableData *myPdfData = [NSMutableData data];
    UIGraphicsBeginPDFContextToData(myPdfData, kPDFPageBounds, nil);
    UIGraphicsBeginPDFPage();
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [self drawStuffInContext:ctx];  // Method also usable from drawRect:.
    UIGraphicsEndPDFContext();
    return myPdfData;
}

- (void)drawStuffInContext:(CGContextRef)ctx {
    UIFont *font = [UIFont fontWithName:@"Zapfino" size:48];
    CGRect textRect = CGRectInset(kPDFPageBounds, 36, 36);
    [@"hello world!" drawInRect:textRect withFont:font];
}


- (IBAction)printFromIphone:(id)sender {
    float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];

    if (systemVersion>4.1) {

        NSData *myPdfData = [NSData dataWithContentsOfFile:myPdfData]; //check the value inside |myPdfData| and |pdfPath| is the path of your pdf.
        UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
        if (controller && [UIPrintInteractionController canPrintData:myPdfData]){
            //controller.delegate = delegate; //if necessary else nil
            UIPrintInfo *printInfo = [UIPrintInfo printInfo];
            printInfo.outputType = UIPrintInfoOutputGeneral;
            printInfo.jobName = [myPdfData lastPathComponent];
            //printInfo.duplex = UIPrintInfoDuplexLongEdge;
            controller.printInfo = printInfo;
            controller.showsPageRange = YES;
            controller.printingItem = myPdfData;

            // We need a completion handler block for printing.
            UIPrintInteractionCompletionHandler completionHandler = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
                if(completed && error){
                    NSLog(@"FAILED! due to error in domain %@ with error code %u", error.domain, error.code);
                }
            };

            //            [controller presentFromRect:CGRectMake(200, 300, 100, 100) inView:senderView animated:YES completionHandler:completionHandler];
        }else {
            NSLog(@"Couldn't get shared UIPrintInteractionController!");
        }
    }
}
4

1 に答える 1

0

タイプミスかどうかはわかりませんが、実際の pdfData をコメントアウトしたため、宣言されていません。

が必要なため、この行のコメントを外す必要がありmyPdfDataます。

//NSData *myPdfData = [NSData dataWithContentsOfFile:pdfData]; //check the value inside |myPdfData| and |pdfPath| is the path of your pdf.

ファイルの代わりに YOUR pdf を使用するには、この行に置き換えることができます。

NSData *myPdfData = [self generatePDFDataForPrinting];
于 2013-01-17T13:36:44.623 に答える