2016-06-21 10 views
0

私はコードの簡単なスニペットに苦労しています。私は多くの時間を探してチェックフォーラムをチェックし、さまざまなことを試しました。私はほとんどそこにいるが、それほどではない。基本的に私はUITableViewの内容をPDFに変換する必要があります。Convertir UITableView to PDF

私がすぐ下にあるコードは、ユーザーがを持っていることを除いてうまく動作しているようですが、その部分はレンダリングされません。私はプログラムでUITableViewをスクロールしましたが、それはトリックをやっているようには見えません。あなたは私が今使っているコードを固定するための提案をお持ちの場合は持って起こるあなたのこと、これは

-(void) createPDFfromUITable:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename 
{ 
    CGRect priorBounds = self.tableView.bounds; 
    CGSize fittedSize = [self.tableView sizeThatFits:CGSizeMake(priorBounds.size.width,3000)]; 
    self.tableView.bounds = CGRectMake(0, 0, fittedSize.width,1800); 

    CGRect pdfPageBounds = CGRectMake(0, 0,self.view.bounds.size.width,792); 
    NSMutableData *pdfData = [[NSMutableData alloc] init]; 

    UIGraphicsBeginPDFContextToData(pdfData, pdfPageBounds, nil); 
    CGFloat pageOriginY=0; 
    for(int i=0;i<2;i++)//for (CGFloat pageOriginY = 0; pageOriginY < fittedSize.height; pageOriginY += pdfPageBounds.size.height) 
    { 
     UIGraphicsBeginPDFPageWithInfo(pdfPageBounds, nil); 
     CGContextSaveGState(UIGraphicsGetCurrentContext()); 
     { 
      CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, -pageOriginY); 
      [self.tableView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
      [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:([_resultados count]-1)] atScrollPosition:UITableViewScrollPositionTop animated:NO]; 
      CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, -pageOriginY); 

     } CGContextRestoreGState(UIGraphicsGetCurrentContext()); 

     pageOriginY += pdfPageBounds.size.height; 
    } 

    UIGraphicsEndPDFContext(); 

    self.tableView.bounds = priorBounds; // Reset the tableView 

    NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsPath = [paths objectAtIndex:0]; 
    NSString *filePathPDF = [documentsPath stringByAppendingPathComponent:aFilename]; 
    [pdfData writeToFile:filePathPDF atomically:YES]; 
    [self showEmail:filePathPDF]; 
} 

:-)昨日のために固定私はQuartz2Dに、より多くの、さらにこのようなものを理解するために取得していますが、クライアントが必要すべてのことを行う全体のスニペット、これは非常に高く評価されるだろう。

答えて

1

私は、ユーザーが入力したデータをエクスポートして、その情報を含むpdfレポートを生成したいという同様の状況に遭遇しました。

研究の日の後、私はUITableViewをスクリーンショットするのは良い習慣ではないことが分かった。

CoreTextを使用して独自に描画することをおすすめします。ここでは、描画を行うヘルパーメソッドがあります。

+(void)drawText:(NSString*)textToDraw withFont:(UIFont *)font inFrame:(CGRect)frameRect 
{ 
    if (textToDraw) { 
     // create attributed string 
     CFMutableAttributedStringRef attrStr = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0); 
     CFAttributedStringReplaceString (attrStr, CFRangeMake(0, 0), (CFStringRef) textToDraw); 

     // create font 
     if (!font) { 
      font = [UIFont fontWithName:@"TimesNewRomanPSMT" size:12]; 
     } 
     CFStringRef fontString = (__bridge CFStringRef)font.fontName; 

     CTFontRef fontRef = CTFontCreateWithName(fontString, font.pointSize, NULL); 

     // create paragraph style and assign text alignment to it 
     CTTextAlignment alignment = kCTTextAlignmentLeft; 
     CTParagraphStyleSetting _settings[] = { {kCTParagraphStyleSpecifierAlignment, sizeof(alignment), &alignment} }; 
     CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(_settings, sizeof(_settings)/sizeof(_settings[0])); 

     // set paragraph style attribute 
     CFAttributedStringSetAttribute(attrStr, CFRangeMake(0, CFAttributedStringGetLength(attrStr)), kCTParagraphStyleAttributeName, paragraphStyle); 

     // set font attribute 
     CFAttributedStringSetAttribute(attrStr, CFRangeMake(0, CFAttributedStringGetLength(attrStr)), kCTFontAttributeName, fontRef); 

     // release paragraph style and font 
     CFRelease(paragraphStyle); 
     CFRelease(fontRef); 

     // Prepare the text using a Core Text Framesetter 

     CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrStr); 


     CGMutablePathRef framePath = CGPathCreateMutable(); 
     CGPathAddRect(framePath, NULL, frameRect); 

     // Get the frame that will do the rendering. 
     CFRange currentRange = CFRangeMake(0, 0); 
     CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL); 
     CGPathRelease(framePath); 

     // Get the graphics context. 
     CGContextRef currentContext = UIGraphicsGetCurrentContext(); 

     // Put the text matrix into a known state. This ensures 
     // that no old scaling factors are left in place. 
     CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity); 


     // Core Text draws from the bottom-left corner up, so flip 
     // the current transform prior to drawing. 
     CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2); 
     CGContextScaleCTM(currentContext, 1.0, -1.0); 

     // Draw the frame. 
     CTFrameDraw(frameRef, currentContext); 

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



    } 
} 

+(void)drawLineFromPoint:(CGPoint)from toPoint:(CGPoint)to 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetLineWidth(context, 1.0); 

    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB(); 



    CGColorRef color = [UIColor colorWithWhite:0.0 alpha:1.0].CGColor; 

    CGContextSetStrokeColorWithColor(context, color); 


    CGContextMoveToPoint(context, from.x, from.y); 
    CGContextAddLineToPoint(context, to.x, to.y); 

    CGContextStrokePath(context); 
    CGColorSpaceRelease(colorspace); 


} 

+(void)drawTableAt:(CGPoint)origin 
    withRowHeight:(int)rowHeight 
    andColumnWidth:(int)columnWidth 
     andRowCount:(int)numberOfRows 
    andColumnCount:(int)numberOfColumns 

{ 
    for (int i = 0; i <= numberOfRows; i++) { 

     int newOrigin = origin.y + (rowHeight*i); 


     CGPoint from = CGPointMake(origin.x, newOrigin); 
     CGPoint to = CGPointMake(origin.x + (numberOfColumns*columnWidth), newOrigin); 

     [self drawLineFromPoint:from toPoint:to]; 


    } 

    for (int i = 0; i <= numberOfColumns; i++) { 
     int newOrigin = origin.x + (columnWidth*i); 


     CGPoint from = CGPointMake(newOrigin, origin.y); 
     CGPoint to = CGPointMake(newOrigin, origin.y +(numberOfRows*rowHeight)); 

     [self drawLineFromPoint:from toPoint:to]; 


    } 
} 

だからあなたのコードは次のようになります。

// Draw first page 
CGRect pageFrame = CGRectMake(0, 0, 612.0, 792.0); 
UIGraphicsBeginPDFPageWithInfo(pageFrame, nil); 
CGContextScaleCTM(UIGraphicsGetCurrentContext(),pageFrame.size.width/screenSize.width, pageFrame.size.height/screenSize.height); 
[self doTheDrawing]; 
UIGraphicsEndPDFContext();