How to get a snapshot of UITableView in a PDF format

前端 未结 5 475
一向
一向 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条回答
  •  -上瘾入骨i
    2021-01-31 13:18

    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"];
    

    i dont know but this code works like a charm for me..

提交回复
热议问题