这个问题之前已经发布过(从iphone应用程序中的UIScrollView创建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]; documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename]; // instructs the mutable data object to write its context to a file on disk [pdfData writeToFile:documentDirectoryFilename atomically:YES]; }
设置:我有一个UIScrollView,里面是一个UIView.我想保存整个UIView(950,500px),并且它位于(520,500)的空间(UIScrollView大小)中.生成的PDF只是UIScrollView(520,500)的大小
我读了答案,但他显然改变了代码,但它对我不起作用.我一直在努力解决这个问题.
我是初学者,所以请指出我应该添加到我错过的问题中的任何内容.谢谢.
PS - 这是一个iPad应用程序.
上下文应该具有scrollview的内容大小的大小,而不是边界.
然后,您需要暂时将scrollview的大小调整为其内容大小,在PDF上下文中呈现它,并将scrollview的大小恢复为其原始大小.
UIGraphicsBeginPDFContextToData(pdfData, (CGRect){0,0, scrollView.contentSize}, nil); CGRect origSize = scrollView.frame; CGRect newSize = origSize; newSize.size = scrollView.contentSize; [scrollView setFrame:newSize]; [scrollView.layer renderInContext:pdfContext]; [scrollView setFrame:origSize];