If it's possible to generate a PDF file from a UITableView?

与世无争的帅哥 提交于 2019-12-22 06:33:44

问题


My application has a tableview with custom cells(include textfield, label and button), I wanna generate a pdf file from the tableview.

Bellow is the code(now assume the tableview's content is all visible):

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

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

    UIGraphicsEndPDFContext();

I always generate an blank pdf file. Currently what I do is to generate the tableview as a image, then draw the image to the current context, then will get the pdf file. but any nice idea?


回答1:


HI , you can convert current view as image through the follwoing code and then you have to use that image to create PDF File through the 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;
   }



回答2:


To get good quality image,

Use

UIGraphicsBeginImageContextWithOptions(screenRect.size, NO, 0.0) 

instead of

UIGraphicsBeginImageContext(screenRect.size)

@JeffWood: thank a million, for your answer :)



来源:https://stackoverflow.com/questions/5725285/if-its-possible-to-generate-a-pdf-file-from-a-uitableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!