[英]UIView renderInContex specific position
在我的应用程序中,我需要创建一个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);
}
感谢帮助。
最高
只需使用CGContextTranslateCTM
将上下文转换为视图的原点即可。 您可能还想使用drawInContext
来产生矢量化而不是栅格化的输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.