iOS:将 UITableView 渲染成 PDF

标签 ios ipad uitableview pdf-generation core-graphics

我已经编写了 pdfGenerator 类来呈现整个 UITableView 以生成 PDF。

用法:

[pdfGenerator createPDFFromTableview:self.tableView];

类方法:

- (void)createPDFFromTableview:(UITableView *)tableview {
    [tableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
    [self renderTableView:tableview];

    int rows = [tableview numberOfRowsInSection:0];
    int numberofRowsInView = 17;
    for (int i = 0; i < rows/numberofRowsInView; i++) {
        [tableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:(i+1)*numberofRowsInView inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
        [self renderTableView:tableview];
    }
}

- (void)renderTableView:(UITableView*)tableview {
    UIGraphicsBeginImageContext(tableview.frame.size);
    [tableview.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *tableviewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIGraphicsBeginPDFPage();
    [tableviewImage drawInRect:tableview.frame];
}

这段代码可以正确滚动 tableview,但它只为第一页创建 PDF,其余页面都是空白的!我不知道为什么会发生这种情况,而它应该按原样获取当前的 uitableview 快照并呈现但它似乎只对第一页这样做。有人可以指出这里可能有什么问题吗?有没有更好的方法来实现我想要实现的目标?

[编辑]

下面给出相同的结果;

int totalRows = [tableview numberOfRowsInSection:0];
int totalVisibleRows = 17;
for (int i = 0; i < ceil((float)totalRows/totalVisibleRows); i++) {
    [tableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:i * totalVisibleRows inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];

    [pdfGenerator renderTableView:tableview indexPathStart:[NSIndexPath indexPathForRow:i * totalVisibleRows inSection:0] indexPathEnd:[[tableview indexPathsForVisibleRows] lastObject]];
}

- (void)renderTableView:(UITableView*)tableview indexPathStart:(NSIndexPath *)startingIndexPath indexPathEnd:(NSIndexPath *)endIndexPath {
    CGRect rectToDraw = CGRectUnion([tableview rectForRowAtIndexPath:startingIndexPath], [tableview rectForRowAtIndexPath:endIndexPath]);

    NSLog(@"%@", NSStringFromCGRect(rectToDraw));

    UIGraphicsBeginImageContext(rectToDraw.size);
    [tableview.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *tableviewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIGraphicsBeginPDFPage();
    [tableviewImage drawInRect:rectToDraw];
}

输出矩形绘制:

{{0, 0}, {768, 720}}
{{0, 680}, {768, 720}}
{{0, 1360}, {768, 720}}
{{0, 2040}, {768, 360}}

最佳答案

在以编程方式滚动 tableview 之后,我最终渲染了持有 tableview 的 View 。滚动后渲染 tableview 似乎不起作用!

关于iOS:将 UITableView 渲染成 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17771112/

相关文章:

ios - 为什么 shouldAutorotate() 在与 iPhone 一起工作时不影响 iPad?

security - MonoTouch - 如何使用 SecKeyChain.QueryAsRecord() 获取证书列表?

javascript - iPad 上的 Safari 在纵向模式下不适合 980px 宽度的网站

iphone - Facebook 给出安全警告 : Please treat the URL above as you would your password and do not share it with anyone

ios - 来自 IB 的原型(prototype)单元引用

ios - UICollectionView 未正确重新加载

ios - 搜索栏 - 导航栏中的取消按钮

iOS 单元测试 : Wait for Time Interval with Expectations

ios - 重新显示 View 时,另一个表的单元格中的动态大小表不会调整大小

ios - 在 iOS5 中单选时取消选择单元格?