8

我有一个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];
}
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 回答
5

这是不滚动表格视图的方法。只需将滚动视图的高度增加到 contentSize。如果您不通过UIImage. 这是来自我UITableView在 Swift 中的扩展。代码是从这里那里偷来的,所以评论并不总是我的。

func toPDF(fileName: String) -> String {
    // Don't include scroll indicators in file
    self.showsVerticalScrollIndicator = false

    // Creates a mutable data object for updating with binary data, like a byte array
    let pdfData = NSMutableData()

    // Change the frame size to include all data
    let originalFrame = self.frame
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.contentSize.width, self.contentSize.height)

    // Points the pdf converter to the mutable data object and to the UIView to be converted
    UIGraphicsBeginPDFContextToData(pdfData, self.bounds, nil)
    UIGraphicsBeginPDFPage()
    let pdfContext = UIGraphicsGetCurrentContext();

    // Draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
    self.layer.renderInContext(pdfContext!)

    // Remove PDF rendering context
    UIGraphicsEndPDFContext()

    // Retrieves the document directories from the iOS device
    let documentDirectories: NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)

    let documentDirectory = documentDirectories.objectAtIndex(0)
    let documentDirectoryFilename = documentDirectory.stringByAppendingPathComponent(fileName);

    // Instructs the mutable data object to write its context to a file on disk
    pdfData.writeToFile(documentDirectoryFilename, atomically: true)

    // Back to normal size
    self.frame = originalFrame

    // Put back the scroll indicator
    self.showsVerticalScrollIndicator = true

    return documentDirectoryFilename
}
于 2015-10-08T15:29:12.713 回答
2

没有屏幕外单元格,因为它们一旦滚动出可见屏幕就会被回收。您应该考虑使用 Core Text 创建 PDF 版本,而不是截屏 UITableView。也许你可以修改这个例子

于 2013-04-16T11:39:22.807 回答
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 回答
0

我想每个人都忘记了那毕竟UITableView是一个。UIScrollView无需计算单元格、高度等。您可以简单地在上下文中使用contentSize、设置tableView.frame.size = tableView.contentSize和渲染来创建上下文。

请看这个例子

        UIGraphicsBeginImageContext(tableView.contentSize)
        let context = UIGraphicsGetCurrentContext()

        let cachedOffset = CGPoint(x:tableView.contentOffset.x , y:tableView.contentOffset.y)
        let cachedFrame = tableView.frame

        tableView.contentOffset = .zero
        tableView.frame = CGRect(x: 0, y: 0, width: tableView.contentSize.width, height: tableView.contentSize.height)
        tableView.layer.render(in: context)

        tableView.frame = cachedFrame
        tableView.contentOffset = cachedOffset

        if let image = UIGraphicsGetImageFromCurrentImageContext() {
            let imageView = UIImageView(image: image)
            self.createPDF(fromUIViews:myImage saveToDocumentsWithFileName:"PDF Name")
        }

        UIGraphicsEndImageContext()
于 2017-04-27T13:00:45.240 回答