我正在尝试将UIScrollView的所有内容保存为.pdf。我找到了一些用于保存当前视图的教程,它们都依赖于使用UIGraphicsGetCurrentContext()
创建CGContextRef。现在,我只用
CGContextRef context = UIGraphicsGetCurrentContext();
[myView.layer renderInContext:context];
这适用于简单的UIView。但是,当我尝试将UIScrollView作为myView
传递时,它会传递UIScrollView的可见部分,但屏幕外的任何内容都只是空格。 有没有办法可以以某种方式扩展context
以获取我的UIScrollView中的所有内容,而不仅仅是当前可见的内容?我怀疑我无法使用{{1}对于UIScrollView,但我不知道该使用什么,而the Apple docs对此并不是非常有帮助。
答案 0 :(得分:3)
如果您有一个子视图,使用滚动内容获取scrollView的整个内容大小,您可以这样做:
CGContextRef context = UIGraphicsGetCurrentContext();
UIView *contentView = [scrollView subviews][0];
[contentView.layer renderInContext:context];
如果scrollView中有多个视图,您可以这样做:
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(
scrollView.frame.origin.x,
scrollView.frame.origin.y,
scrollView.contentSize.width,
scrollView.contentSize.height)];
for(UIView *view in [scrollView subviews]){
[contentView addSubview: view];
}
CGContextRef context = UIGraphicsGetCurrentContext();
[contentView.layer renderInContext:context];
然后您需要将视图返回到scrollView。可能有一种方法来复制它们,但我不知道如何。无论如何这应该是有用的:
for(UIView *view in [contentView subviews]){
[view removeFromSuperView];
[scrollView addSubview:view];
}
答案 1 :(得分:0)
在调用renderInContext之前,您可以将UIScrollView的帧设置为等于它的内容大小,将内容偏移设置为0。之后将帧恢复为原始。
myView.frame = CGRectMake(myView.frame.origin.x,myView.frame.origin.y,myView.contentSize.with,myView.contentSize.height);
答案 2 :(得分:0)
这个代码我曾经从uiscrollview制作pdf并且它确实有帮助,但它不会像我们使用pdf那样好 - 请看看 - 来自UIScrollView的Pdf
- (void) createPDF
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *directroyPath = nil;
directroyPath = [documentsDirectory stringByAppendingPathComponent:@"temp"];
NSString *filePath = [directroyPath stringByAppendingPathComponent:@"test.pdf"];
// check for the "PDF" directory
NSError *error;
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
} else {
[[NSFileManager defaultManager] createDirectoryAtPath:directroyPath
withIntermediateDirectories:NO
attributes:nil
error:&error];
}
CGContextRef pdfContext = [self createPDFContext:_scrollView2.bounds path:(CFStringRef)filePath];
NSLog(@"PDF Context created");
/*
Here limit of i is no of pages in your uiscrollview or
you can use hit and trial here because there is a
length of page is going to be equal to
the visible size of uiscrollView in your application
*/
for (int i = 0 ; i< 2 ; i++)
{
// page 1
CGContextBeginPage (pdfContext,nil);
//turn PDF upsidedown
CGAffineTransform transform = CGAffineTransformIdentity;
//here 365 is equal to the height of myScrollView.frame.size.height
transform = CGAffineTransformMakeTranslation(0, (i+1) * 365);
transform = CGAffineTransformScale(transform, 1.0, -1.0);
CGContextConcatCTM(pdfContext, transform);
//Draw view into PDF
[_scrollView2.layer renderInContext:pdfContext];
CGContextEndPage (pdfContext);
[_scrollView2 setContentOffset:CGPointMake(0, (i+1) * 365) animated:NO];
}
CGContextRelease (pdfContext);
}
- (CGContextRef) createPDFContext:(CGRect)inMediaBox path:(CFStringRef) path
{
CGContextRef myOutContext = NULL;
CFURLRef url;
url = CFURLCreateWithFileSystemPath (NULL, path,
kCFURLPOSIXPathStyle,
false);
if (url != NULL) {
myOutContext = CGPDFContextCreateWithURL (url,
&inMediaBox,
NULL);
CFRelease(url);
}
return myOutContext;
}
答案 3 :(得分:0)
检查ScrollViewToPDF示例并了解它。
它使用相同的scrollview's
layer
renderInContext
,但此处PDF是根据您的要求创建的,例如一页PDF或多页PDF