2012-03-21 19 views
1

私のアプリでは、PDFを作成する必要があります。このPDFには、ユーザーがテキストを内部に書き込むいくつかのボックスがあるレイアウトが必要です。UIView renderInContex特定の位置

私の問題は、レイヤーが常に0,0(x、y)の原点を持ちながら、pdfの特定の位置にボックスを描画したいということです。助けのための

-(IBAction)genPdf:(id)sender { 
    UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 100) ]; 
    aView.layer.borderColor = [UIColor redColor].CGColor; 
    aView.layer.borderWidth = 2; 
    aView.layer.cornerRadius = 8;   

    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString *filename = @"test.pdf"; 
    NSURL *fileURL = [NSURL fileURLWithPathComponents:[NSArray arrayWithObjects:documentsDirectory, filename, nil]]; 

    // Create PDF context 
    CGRect pdfSize = CGRectMake(0, 0, 420, 596); 
    CGContextRef pdfContext = CGPDFContextCreateWithURL((CFURLRef)fileURL, &pdfSize, NULL); 
    CGPDFContextBeginPage(pdfContext, NULL); 
    UIGraphicsPushContext(pdfContext); 
    // Flip coordinate system 
    CGRect bounds = CGContextGetClipBoundingBox(pdfContext); 
    CGContextScaleCTM(pdfContext, 1.0, -1.0); 
    CGContextTranslateCTM(pdfContext, 0.0, -bounds.size.height); 

    // Drawing commands 
    [aView.layer renderInContext:pdfContext]; // should be origin.x and .y 10,10 but is 0,0!!!! 

    // Clean up 
    UIGraphicsPopContext(); 
    CGPDFContextEndPage(pdfContext); 
    CGPDFContextClose(pdfContext); 

} 

ありがとう:

は、ここに私のコードです。

マックス

答えて

0

だけCGContextTranslateCTMとビューの原点にコンテキストを翻訳します。ラスタライズされた出力ではなくベクトル化された出力を生成するために、drawInContextを使用することもできます。

+0

UIViewsの境界を変更する簡単な方法が見つかりました。 – masgar

関連する問題