此问题已在(Creating PDF from UIScrollView in iphone app)之前发布,我使用的代码是here.
这是代码
-(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)的大小
我读了answer,但他显然改变了代码,但它对我不起作用.我一直在努力解决这个问题.
我是初学者,所以请指出我应该添加到我错过的问题中的任何内容.谢谢.
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];
相关文章
转载注明原文:ios – 从UIScrollView目标C创建PDF - 代码日志