How to get a snapshot of UITableView in a PDF format

前端 未结 5 482
一向
一向 2021-01-31 12:22

I have a UITableView which is populated with some data but since it contains data that is more cells than the viewable area of the screen, I only managed to get onl

5条回答
  •  悲&欢浪女
    2021-01-31 13:10

    Based on Vin's answer (also can be used for snapshotting UIScrollViews):

        UIGraphicsBeginPDFContextToData(pdfData, CGRectMakeWithSize(0, 0, self.productsTable.contentSize), nil);
        UIGraphicsBeginPDFPage();
        [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
        [self.productsTable scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
        [self.productsTable.layer renderInContext:UIGraphicsGetCurrentContext()];
    
        CGFloat screensInTable = self.productsTable.contentSize.height / self.productsTable.height;
    
        for (int i = 1; i < screensInTable; i++) {
            CGPoint contentOffset = CGPointMake(0, i * self.productsTable.height);
            [self.productsTable setContentOffset:contentOffset animated:NO];
            [self.productsTable.layer renderInContext:UIGraphicsGetCurrentContext()];
        }
    
        UIGraphicsEndPDFContext();
    

提交回复
热议问题