8

いくつかのデータが取り込まれてUITableViewいますが、画面の表示可能領域よりも多くのセルのデータが含まれているため、そのスナップショットしか取得できませんでした。取得できる他の方法があるかどうかを知りたいですpdfのテーブルビュースナップショット全体..これは私が試したものです

- (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];
}
4

5 に答える 5

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

わかりませんが、このコードは私にとって魅力のように機能します..

于 2013-04-16T12:25:58.740 に答える
2

Vin の回答に基づく (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();
于 2014-10-23T01:51:25.383 に答える