我正在使用代码创建PDF文件。的工作原理。
但是:我希望我的PDF文件中的整个UITableView(我需要滚动),而不仅仅是当前显示在屏幕上的视图部分。 另外我需要将三个textField添加到同一个文件PDF中 这必须是视图的所有内容(三个textField和tableView与所有单元格)因为这是一张发票 有没有办法实现这个目标? 我使用以下代码
- (void)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, self.view.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
[self.view.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];
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
}
但是它作为图像的视图和tableView的剩余单元格所包含的问题不包括PDF文件
答案 0 :(得分:0)
UIView *viewToRender = self.tableView;
CGPoint contentOffset = self.tableView.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();
UIGraphicsEndImageContext();
try this