将整个 uiview 保存为 PDF



我知道如何将 uiview 保存在 PDF 中,但我的问题是如何将整个 uiview 保存在 PDF 中,例如,如果您有一个比 iPad 显示更长的页面并且您需要滚动它,我只能将活动视图保存在 PDF 文件中,但其余的不存在。我正在寻找帮助或任何对我有帮助的教程。

提前谢谢。

您可以像这样将视图呈现为 PDF:

#define kDefaultPageHeight 1024
#define kDefaultPageWidth  768
#define kMargin 5
CGRect origframe = someView.frame;
NSString *heightStr = origFrame.size.height;
int height = [heightStr intValue];
// Size of the view in the pdf page
CGFloat maxHeight   = kDefaultPageHeight - 2*kMargin;
CGFloat maxWidth    = kDefaultPageWidth - 2*kMargin;
int pages = ceil(height / maxHeight); // or use 1 if you only have 1 page.
UIGraphicsBeginPDFContextToFile(@"some/full/path.pdf", CGRectZero, nil);
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, kDefaultPageWidth, kDefaultPageHeight), nil);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(currentContext, kMargin, kMargin);
/* this is the view that you will render to the PDF */
[someView.layer renderInContext:currentContext];
UIGraphicsEndPDFContext();

当然,这是一个粗略的代码片段,您需要进行调整以满足您的需求

最新更新