ios - 如何获取 PDF 格式的 UITableView 快照

标签 ios uitableview pdf-generation

我有一个 UITableView,其中填充了一些数据,但由于它包含的数据比屏幕的可视区域多,所以我只设法获取它的快照,我想要想知道是否有任何其他方法可以获取 pdf 格式的整个 tableview 快照。这就是我尝试过的方法,谢谢

- (IBAction)clickMe:(id)sender
{
    UIView *viewToRender = self.myTableView;
    CGPoint contentOffset = self.myTableView.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();
    UIImageView *myImage=[[UIImageView alloc]initWithImage:image];

    UIGraphicsEndImageContext();

    [self createPDFfromUIViews:myImage saveToDocumentsWithFileName:@"PDF Name"];

}



- (void)createPDFfromUIViews:(UIView *)myImage saveToDocumentsWithFileName:(NSString *)string
{
    NSMutableData *pdfData = [NSMutableData data];

    UIGraphicsBeginPDFContextToData(pdfData, myImage.bounds, nil);
    UIGraphicsBeginPDFPage();
    CGContextRef pdfContext = UIGraphicsGetCurrentContext();


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

    [myImage.layer renderInContext:pdfContext];

    // remove PDF rendering context
    UIGraphicsEndPDFContext();
    NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

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

    NSLog(@"%@",documentDirectoryFilename);
    [pdfData writeToFile:documentDirectoryFilename atomically:YES];
}

最佳答案

UIGraphicsBeginImageContext(self.myTableView.contentSize);
[self.myTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
[self.myTableView.layer renderInContext:UIGraphicsGetCurrentContext()];

int rows = [self.myTableView numberOfRowsInSection:0];
int numberofRowsInView = 4;
for (int i =0; i < rows/numberofRowsInView; i++) {
    [self.myTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:(i+1)*numberofRowsInView inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
    [self.myTableView.layer renderInContext:UIGraphicsGetCurrentContext()];

}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIImageView *myImage=[[UIImageView alloc]initWithImage:image];
UIGraphicsEndImageContext();


[self createPDFfromUIViews:myImage saveToDocumentsWithFileName:@"PDF Name"];

我不知道,但这段代码对我来说就像一个魅力......

关于ios - 如何获取 PDF 格式的 UITableView 快照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16035685/

相关文章:

uitableview - 如何在uitableview中使用计时器?

ios - 自定义 UITableViewCell、UITableView 和 allowsMultipleSelectionDuringEditing

javascript - 截取 React 应用程序的屏幕截图并将其生成为 PDF

ios - 显示信息标记窗口后更新图像

iphone - 如何在 iOS 应用程序中编辑 PDF 文件?

ios - 不删除导航后退按钮标题

ios - 如何在真实的ios设备上运行带有热重载的react-native应用程序?

ios - 如果我使用 setter,UIStoryboard 中的对象将变为 nil

iphone - iOS:在pdf生成中绘制文本时如何设置字体?

java - 在java中使用Itext仅将标题添加到第一个PDF页面