Objective c 如何为uitableview中标题为的特定节生成pdf?

Objective c 如何为uitableview中标题为的特定节生成pdf?,objective-c,uitableview,Objective C,Uitableview,我尝试过这种方法,但在第一部分也不起作用 - (NSMutableData*)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename { // Creates a mutable data object for updating with binary data, like a byte array NSMutableData *pdfData = [NSMutable

我尝试过这种方法,但在第一部分也不起作用

- (NSMutableData*)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{
    // Creates a mutable data object for updating with binary data, like a byte array
    NSMutableData *pdfData = [NSMutableData data];

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

    // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData

    [aView.layer renderInContext:pdfContext];

    // remove PDF rendering context
    UIGraphicsEndPDFContext();

    // Retrieves the document directories from the iOS device
    NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

    NSString* documentDirectory = [documentDirectories objectAtIndex:0];
    NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];

    // instructs the mutable data object to write its context to a file on disk
    [pdfData writeToFile:documentDirectoryFilename atomically:YES];
    NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
    return pdfData;
}
- (void)renderTableView:(UITableView*)tableview indexPathStart:(NSIndexPath *)startingIndexPath indexPathEnd:(NSIndexPath *)endIndexPath {
    CGRect rectToDraw = CGRectUnion([tableview rectForRowAtIndexPath:startingIndexPath], [tableview rectForRowAtIndexPath:endIndexPath]);

    NSLog(@"%@", NSStringFromCGRect(rectToDraw));

    UIGraphicsBeginImageContext(rectToDraw.size);
    [tableview.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *saveImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [saveImage drawInRect:rectToDraw];
    imageData = UIImagePNGRepresentation(saveImage);
}

-(void)sendSectionpdfMail{
    [ExpenseseTableview reloadData];

    [self renderTableView:ExpenseseTableview indexPathStart:[NSIndexPath indexPathForRow:0 inSection:sectionTag] indexPathEnd:[NSIndexPath indexPathForRow:1 inSection:sectionTag]];

    UIImage *image = [UIImage imageWithData:imageData];
    UIImageView *imageView = [[UIImageView alloc]initWithImage:image];

    [self createPDFfromUIView:imageView saveToDocumentsWithFileName:@"Expenses.pdf"];

}

为特定部分生成带有标题的PDF是个坏主意,这也会影响应用程序的性能,相反,您可以使用HTML查看器显示数据。如果您需要PDF格式的输出,您可以轻松地将HTML转换为PDF。好的,谢谢。我试试看