Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a snapshot of UITableView in a PDF format

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 only the snapshot of it, I want to know if there is any other way I can get the whole tableview snapshot in pdf..this is what I have tried thanks

- (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];
}
like image 486
vin Avatar asked Apr 16 '13 11:04

vin


3 Answers

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..

like image 194
vin Avatar answered Oct 18 '22 10:10

vin


Here's how you can do it without scrolling the tableview. Simply increase the height of the scrollview to the contentSize. The quality of the PDF is also better if you don't go via a UIImage. This is from my UITableView extension in Swift. The code is stolen from here and there so the comments aren't always mine.

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
}
like image 5
pierrovic Avatar answered Oct 18 '22 12:10

pierrovic


There are no offscreen cells because they are recycled as soon as they scroll out of the visible screen. Instead screenshoting the UITableView, you should consider creating a PDF version using Core Text. Maybe you can adapt this example.

like image 2
Jano Avatar answered Oct 18 '22 12:10

Jano