2012-03-21 42 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 2012-03-21 13:29:33