2012-09-25 107 views
0

任何人都可以帮助我什么是UIView为A4尺寸纸张的图纸的大小。用于绘图的UiView尺寸是多少?A4尺寸纸张的文件是多少?

我使用下面的大小,但我的pdfView对象像标签,图像不是我提供绘图的大小。

我创建.xib并手动绘制UILabels,图像和线然后使用标签属性我为绘图提供了框架,但它们根据.xib绘制了不同的位置我提供了与.xib(0, 0,792,1122)。

UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 792, 1122), nil); 

+(void)drawLabels:(NSMutableArray*)arr isData:(BOOL)isdata 
{ 

    NSArray* objects = [[NSBundle mainBundle] loadNibNamed:@"PDFView" owner:nil options:nil]; 

    UIView* mainView = [objects objectAtIndex:0]; 


    if (isdata) 
    { 
     for (int i=1;i<=[arr count];i++) 
     { 
      UILabel *lbl=(UILabel*)[mainView viewWithTag:i]; 
      [self drawText:[arr objectAtIndex:i-1] inFrame:lbl.frame isData:isdata]; 
     } 
    } 
} 

//这里是我的Draw方法

+(void)drawText:(NSString*)textToDraw inFrame:(CGRect)frameRect isData:(BOOL)isdata 
{ 
    int length=[textToDraw length]; 
    CFStringRef string = (__bridge CFStringRef) textToDraw; 
    CFMutableAttributedStringRef currentText = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0); 
    CTFontRef font = CTFontCreateWithName((CFStringRef)@"Helvetica Neue Bold", 12.0f, nil); 
    if (isdata) 
    { 
     font = CTFontCreateWithName((CFStringRef)@"Helvetica Neue ", 12.0f, nil); 
    } 
    CFAttributedStringReplaceString (currentText,CFRangeMake(0, 0), string); 
    CFAttributedStringSetAttribute(currentText,CFRangeMake(0, length),kCTFontAttributeName,font); 
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText); 
    CGMutablePathRef framePath = CGPathCreateMutable(); 
    CGPathAddRect(framePath, NULL, frameRect); 
    CFRange currentRange = CFRangeMake(0, 0); 
    CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL); 
    CGPathRelease(framePath); 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 
    CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity); 
    CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2); 
    CGContextScaleCTM(currentContext, 1.0, -1.0); 
    CTFrameDraw(frameRef, currentContext); 
    CGContextScaleCTM(currentContext, 1.0, -1.0); 
    CGContextTranslateCTM(currentContext, 0, (-1)*frameRect.origin.y*2); 
    CFRelease(frameRef); 
    CFRelease(framesetter); 
} 

//画线,我使用下面的代码

for (int j=1001;j<=1003;j++) 
     { 
      UILabel *lbl=(UILabel*)[mainView viewWithTag:j]; 

      CGPoint lblPoint=CGPointMake(lbl.frame.origin.x, lbl.frame.origin.y); 
      CGPoint lblPoint2=CGPointMake(lbl.frame.origin.x+lbl.frame.size.width, lbl.frame.origin.y); 
      [self drawLineFromPoint:lblPoint toPoint:lblPoint2]; 
     } 

+(void)drawLineFromPoint:(CGPoint)from toPoint:(CGPoint)to 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetLineWidth(context, 2.0); 
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB(); 
    CGFloat components[] = {0.2, 0.3, 0.2, 0.4}; 
    CGColorRef color = CGColorCreate(colorspace, components); 
    CGContextSetStrokeColorWithColor(context, color); 
    CGContextMoveToPoint(context, from.x, from.y); 
    CGContextAddLineToPoint(context, to.x, to.y); 
    CGContextStrokePath(context); 
    CGColorSpaceRelease(colorspace); 
    CGColorRelease(color); 
} 

这里是我的.xib enter image description here

这里是我的.pdf文件绘制

enter image description here

+1

A4纸的尺寸是595 x 842点,而不是792 x 1122. –

+0

所有标签都是直接指定'mainView'的子视图吗? –

+0

是感谢您的时间马丁先生。 –

回答

2

如果已经下载从项目http://www.raywenderlich.com/6818/how-to-create-a-pdf-with-quartz-2d-in-ios-5-tutorial-part-2,和似乎可以改进在drawText:方法中的CTM计算。问题是修改矩阵时不考虑矩形的高度。

如果更换

CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2); 
... 
CGContextTranslateCTM(currentContext, 0, (-1)*frameRect.origin.y*2); 

CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2 + frameRect.size.height); 
... 
CGContextTranslateCTM(currentContext, 0, (-1)*frameRect.origin.y*2 - frameRect.size.height); 

那么结果似乎更准确。

而且Xcode的静态分析报告中该方法所currentText内存泄漏,所以你应该添加

CFRelease(currentText); 

drawText:结束。

+0

感谢您的时间,但马丁它只是绘制还原,屏幕标志正下方和文字也恢复(反映形式)和某处正确。当我评论CGContextTranslateCTM(currentContext,0,frameRect.origin.y * 2 .....然后它绘制标签,但不是我提供一些大小的第一页上的第二页...... –

+0

@Pandey_Laxman:有你替换了* CGContextTranslateCTM的两个*实例吗?我已经用Ray Wenderlich示例项目对它进行了测试,结果看起来不错。 - 在你的项目中没有源代码*如果你想*你可以制作一个缩小的项目,显示你的问题,并把它放在我可以下载它的地方,我会看看它 –

+0

马丁我的Skype帐号是:pandey.ios请加我 –

1

执行此操作后:

NSArray *aArrPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) ; 

NSString *aStrPrintPdfPath = [[aArrPaths objectAtIndex:0] stringByAppendingPathComponent:@"MyDocument.pdf"]; 
CGContextRef aCgPDFContextRef = [self createPDFContext:CGRectMake(0, 0, 612, 892) path:(__bridge_retained CFStringRef)aStrPrintPdfPath]; 

CGContextBeginPage(aCgPDFContextRef,nil); 
    //turn PDF upsidedown 
    CGAffineTransform aCgAffTrans = CGAffineTransformIdentity; 
    aCgAffTrans = CGAffineTransformMakeTranslation(0,892); 
    aCgAffTrans = CGAffineTransformScale(aCgAffTrans, 1.0, -1.0); 
    CGContextConcatCTM(aCgPDFContextRef, aCgAffTrans); 

//capture whole view screenshot 
[self.view.layer renderInContext:aCgPDFContextRef]; //add QuartzCore framework and import it 
    [aPageNote release]; 
    CGContextEndPage (aCgPDFContextRef); 

CGContextRelease (aCgPDFContextRef); 
NSLog(@"Pdf Successfully Created"); 

而且添加这个方法:

-(CGContextRef) createPDFContext:(CGRect)aCgRectinMediaBox path:(CFStringRef) aCfStrPath 
{ 

    CGContextRef aCgContextRefNewPDF = NULL; 
    CFURLRef aCfurlRefPDF; 
    aCfurlRefPDF = CFURLCreateWithFileSystemPath (NULL,aCfStrPath,kCFURLPOSIXPathStyle,false); 
    if (aCfurlRefPDF != NULL) { 
    aCgContextRefNewPDF = CGPDFContextCreateWithURL (aCfurlRefPDF,&aCgRectinMediaBox,NULL); 
    CFRelease(aCfurlRefPDF); 
    } 
    return aCgContextRefNewPDF; 
} 
+0

但是我有很多数据,所以我需要为多个页面创建pdf。在这个视图中,我需要添加表格单元格并在pdf的newt页面中重复它。我前段时间应该可以搜索到pdf,我尝试使用屏幕截图和滚动可视视图的方式来创建它,但是我的资深人员拒绝了没有搜索和选择文本。 –