//此方法只是把当前页面的内容生成PDF并保存在沙盒中。
//还需要做:把当前面没有显示的内容以分页的形式生成PDF,并把PDF读取并显示出来
//关于显示可以参考:念茜的博客 iOS开发笔记——PDF的显示和浏览 (http://blog.csdn.net/yiyaaixuexi/article/details/7645725)
//Create empty PDF context on iPhone for later randering in it
-(CGContextRef) createPDFContext:(CGRect)inMediaBox path:(CFStringRef) path
{
CGContextRef myOutContext = NULL;
CFURLRef url;
url = CFURLCreateWithFileSystemPath (NULL, // 1
path,
kCFURLPOSIXPathStyle,
false);
if (url != NULL) {
myOutContext = CGPDFContextCreateWithURL (url,// 2
&inMediaBox,
NULL);
CFRelease(url);// 3
}
return myOutContext;// 4
}
/**
* 把当前页面绘制成PDF并保存
*/
- (void)drawPDF{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//PDF保存的路径
NSLog(@"documentsDirectory:%@",documentsDirectory);
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"tmp.pdf"];
CGContextRef pdfContext = [self createPDFContext:self.view.bounds path:(CFStringRef)writableDBPath];
NSLog(@"PDF Context created");
CGContextBeginPage (pdfContext,nil); // 6
//turn PDF upsidedown
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformMakeTranslation(0, self.view.bounds.size.height);
transform = CGAffineTransformScale(transform, 1.0, -1.0);
CGContextConcatCTM(pdfContext, transform);
//Draw view into PDF
[self.view.layer renderInContext:pdfContext];
CGContextEndPage (pdfContext);// 8
CGContextRelease (pdfContext);
}