Is it possible to create a PDF file from a UITableView?

My application has a table view with custom cells (including a text box, shortcut and button), I want to generate a PDF file from a table.

Below is the code (now suppose the contents of the table are viewed):

    UIGraphicsBeginImageContext(self._tableView.bounds.size);
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, self._tableView.bounds.size.width, self._tableView.bounds.size.height), nil);

    // what the code here?
    // [self._tableView drawInRect:];

    UIGraphicsEndPDFContext();

I always create an empty PDF file. I am currently creating a tableview view as an image, then drawing the image in the current context, and then getting the pdf file. but any good idea?

+5
source share
2 answers

HI, you can convert the current view as an image through the following code, and then you must use this image to create a PDF file through a link

    - (UIImage *)captureView:(UIView *)view {
    CGRect screenRect = [[UIScreen mainScreen] bounds];

    UIGraphicsBeginImageContext(screenRect.size);

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [[UIColor blackColor] set];
    CGContextFillRect(ctx, screenRect);

    [view.layer renderInContext:ctx];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return newImage;
   }
+5

,

UIGraphicsBeginImageContextWithOptions(screenRect.size, NO, 0.0) 

UIGraphicsBeginImageContext(screenRect.size)

@JeffWood: , :)

-1

All Articles