【问题标题】:how to insert textField with tableView into PDF file如何将带有tableView的textField插入PDF文件
【发布时间】:2014-04-21 21:56:54
【问题描述】:

我正在使用代码创建 PDF 文件。有效。

但是:我希望我的整个 UITableView(我需要滚动)在我的 PDF 文件中,而不仅仅是当前显示在屏幕上的视图部分。 套索我需要将三个文本字段添加到同一个文件 PDF 这必须取所有视图的内容(三个 textField 和所有单元格的 tableView),因为这是一张发票 有没有办法做到这一点? 我使用以下代码

- (void)saveToDocumentsWithFileName:(NSString*)aFilename;

{
    // Creates a mutable data object for updating with binary data, like a byte array
    NSMutableData *pdfData = [NSMutableData data];

    // Points the pdf converter to the mutable data object and to the UIView to be converted
    UIGraphicsBeginPDFContextToData(pdfData, self.view.bounds, nil);
    UIGraphicsBeginPDFPage();
    CGContextRef pdfContext = UIGraphicsGetCurrentContext();


    // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData

    [self.view.layer renderInContext:pdfContext];

    // remove PDF rendering context
    UIGraphicsEndPDFContext();

    // Retrieves the document directories from the iOS device
    NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

    NSString* documentDirectory = [documentDirectories objectAtIndex:0];
    NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];


    [pdfData writeToFile:documentDirectoryFilename atomically:YES];

}

但它作为视图的图像和 tableView 的剩余单元格不包含 PDF 文件的问题

【问题讨论】:

  • UIGraphicsBeginPDFContextToData(pdfData, self.tablview.bounds, nil);试试这个。
  • 不工作还为现有单元格截屏

标签: ios iphone uitableview pdf-generation core-graphics


【解决方案1】:
UIView *viewToRender = self.tableView;
CGPoint contentOffset = self.tableView.contentOffset;

UIGraphicsBeginImageContext(viewToRender.bounds.size);

CGContextRef ctx = UIGraphicsGetCurrentContext();

//  KEY: need to translate the context down to the current visible portion of the tablview
CGContextTranslateCTM(ctx, 0, -contentOffset.y);

[viewToRender.layer renderInContext:ctx];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();


try this

【讨论】:

  • 完全去掉上面的方法,用这段代码?或仅删除第一部分;以及我可以使用这个变量的图像呢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-26
  • 2013-03-25
  • 1970-01-01
  • 2013-09-24
  • 1970-01-01
  • 2011-04-28
相关资源
最近更新 更多