2012-09-25 59 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,我嘗試使用屏幕截圖和滾動可視視圖的方式來創建它,但是我的資深人員拒絕了沒有搜索和選擇文本。 –