UIView renderInContex конкретная позиция - PullRequest
1 голос
/ 21 марта 2012

В моем приложении мне нужно создать PDF.Этот PDF-файл должен иметь макет с несколькими полями, в которые пользователь будет писать текст.

Моя проблема в том, что слои всегда имеют начало 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);

}

Спасибо за помощь.

Макс

1 Ответ

0 голосов
/ 21 марта 2012

Просто переведите контекст в происхождение представления с помощью CGContextTranslateCTM.Вы также можете использовать drawInContext для получения векторизованного, а не растеризованного вывода.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...