IOS之Quartz2D绘图6.PDF文档生成

来源:互联网 发布:前端数据库 编辑:程序博客网 时间:2024/05/01 00:47

使用quartz2d生成pdf文档,我们需要创建pdf文档的上下文

UIGraphicsBeginPDFContextToFile

第一个参数为PDF文档存储路迹,通过下面代码获取document的路径

NSArray*array=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

   //PDF存储路径

   NSString *path=[array[0]stringByAppendingPathComponent:@"token.pdf"];

创建pdf的页面,每一个pdf页面通过下面代码创建

UIGraphicsBeginPDFPage();

 

生成pdf的函数

   NSArray*array=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

   //PDF存储路径

   NSString *path=[array[0]stringByAppendingPathComponent:@"token.pdf"];

   NSLog(@"%@",path);

   //创建pdf上下文

   UIGraphicsBeginPDFContextToFile(path,CGRectZero,nil);

   for (inti=0;i<3;i++) {

       if (i%2==0) {

           //创建pdf页面

           UIGraphicsBeginPDFPage();

       }

       UIImage *image=[UIImageimageNamed:[NSStringstringWithFormat:@"%d.jpg",i+1]];

       [imagedrawInRect:CGRectMake(0, (i%2)*396,306,396)];

   }

   //关闭上下文

   UIGraphicsEndPDFContext();

完整代码下载

0 0
原创粉丝点击