【发布时间】:2012-04-13 18:22:31
【问题描述】:
我知道如何将 uiview 保存为 PDF,但我的问题是如何将整个 uiview 保存为 PDF,例如,如果您的页面比 iPad 显示长并且您需要滚动它,我只能保存PDF 文件中的活动视图,但其余视图不存在。 我正在寻求帮助或任何对我有帮助的教程。
提前致谢。
【问题讨论】:
标签: objective-c xcode user-interface uiviewcontroller xcode4.2
我知道如何将 uiview 保存为 PDF,但我的问题是如何将整个 uiview 保存为 PDF,例如,如果您的页面比 iPad 显示长并且您需要滚动它,我只能保存PDF 文件中的活动视图,但其余视图不存在。 我正在寻求帮助或任何对我有帮助的教程。
提前致谢。
【问题讨论】:
标签: objective-c xcode user-interface uiviewcontroller xcode4.2
您可以像这样将视图呈现为 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();
当然这是一个粗略的 sn-p 代码,您需要根据自己的需要进行调整
【讨论】:
renderInContext 是最佳答案,但如果您使用CALayer 的某些功能,它可能无法正常工作。见Can't capture masks within view layer。