ios-pdf在uiscrollview中生成

在我的iOS应用程序,我列出了滚动视图中的一些数据。 点击button时,页面中的数据以pdf文件格式生成。 以下是一些代码。

- (void)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); } 

从上面的代码,我能够生成一个PDF文件,但我面临以下问题。 如果我有超过50个数据,在我的应用程序,我只能看到前10个数据,并看到其他我需要滚动他们。 在这种情况下,当pdf文件生成时,它只取得前10个数据而不是其他数据。 如何获得其他数据

renderInContext:将只呈现你目前所看到的 – UIScrollView是聪明的,只会在层次结构中有你需要的视图。 你需要一个for-loop来手动滚动和重画。 这可能是棘手的,可能会在滚动后手动调用[aView layoutSubviews]来强制scrollView立即重新排列子视图。

作为steipete说,你可以使用循环renderIncontext – 你需要计算循环numOfLoop = webview.ScrollView.ContentSize.Height / weview.frame.size.height

  • renderInContext for(int i = 0; i <numOfLopp; i ++)

    1.UIGraphicsBeginPDFPage()

    1. 获取当前上下文
    2. renderIncontext

这种方式在我的应用程序中正确运行,但是当numOfLoop储存器遇到BIG问题时 – 您将变慢renderInContext – 内存将增加 – >closures应用程序以这种方式,我如何删除/释放上下文循环?

其他方法,你可以使用https://github.com/iclems/iOS-htmltopdf/blob/master/NDHTMLtoPDF.h生成PDF

@steipete,@all嗨,我试着下面的代码,它会生成多页PDF,但在视图中的可见区域重复的所有页面。 请添加任何进一步的想法,以改善结果。

 -(void)createPDFfromUIView:(UIWebView*)aView saveToDocumentsWithFileName:(NSString*)aFilename { NSString *oldFilePath= [[NSBundle mainBundle] pathForResource:@"ABC" ofType:@"pdf"]; NSMutableData *pdfData = [NSMutableData data]; UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil); CFURLRef url = CFURLCreateWithFileSystemPath (NULL, (CFStringRef)oldFilePath, kCFURLPOSIXPathStyle, 0); CGPDFDocumentRef templateDocument = CGPDFDocumentCreateWithURL(url); CFRelease(url); size_t count = CGPDFDocumentGetNumberOfPages(templateDocument); for (size_t pageNumber = 1; pageNumber <= count; pageNumber++) { UIGraphicsBeginPDFPage(); CGContextRef pdfContext = UIGraphicsGetCurrentContext(); [aView.layer renderInContext:pdfContext]; } CGPDFDocumentRelease(templateDocument); // 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]; NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename); } 

检查ScrollViewToPDF示例

它使用相同的scrollview's layer renderInContext但这里PDF是根据您的要求创build,如一页PDF或多页PDF

注意:它捕获scrollView的所有可见和不可见部分