UIVIEW转PDF文件

最近项目中新增了一个分享pdf文件的的功能,把一个界面以pdf文件的形式分享出去,刚开始接触pdf,没一点思路,各种查资料,中间踩了好多坑。下面我就把自己踩到的坑跟大家分享一下。

    第一步要做的就是把uiview转换成image,然后把image画到pdf的画布上。

1 uiview转image

UIGraphicsBeginImageContext(CGSizeMake(scrollV.contentSize.width, scrollV.contentSize.height));

[scrollV.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage*image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

image = [self clipWithImageRect:CGRectMake(0, 0, image.size.width, image.size.height/2) clipImage:image];

由于图片不是全部都需要,这里我进行了一步切割处理

- (UIImage *)clipWithImageRect:(CGRect)clipRect clipImage:(UIImage *)clipImage;

{

CGImageRef cgRef = clipImage.CGImage;

CGImageRef imageRef = CGImageCreateWithImageInRect(cgRef, clipRect);

UIImage *newImage = [UIImage imageWithCGImage:imageRef];

CGImageRelease(imageRef);

return  newImage;

}

拿到想要的图片之后要做的就是把它画到pdf文件上

//上面的图片 画图用数学坐标系

CGImageRef pageImage = [image CGImage];

CGContextDrawImage(myPDFContext, CGRectMake(0, scrollV.contentSize.height-image.size.height, [image size].width, [image size].height), pageImage);


由于我的需求比较特殊,这个view的底层是scrollview,所以这个scrollview的滚动区域是动态的,不是一个屏幕大小这样直接截个屏幕就行,于是我实验了好久发现,改变scrollview的偏移量在截张然后把这两张剪切一下拼起来就好了。pdf从创建到结束的代码如下:

// 1.创建media box

CGFloat myPageWidth = scrollV.contentSize.width;

CGFloat myPageHeight = scrollV.contentSize.height;

CGRect mediaBox = CGRectMake (0, 0, myPageWidth, myPageHeight);

// 2.设置pdf文档存储的路径

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

NSString *documentsDirectory = paths[0];

NSString *filePath = [documentsDirectory stringByAppendingString:[NSString stringWithFormat:@"%@%@.pdf",@"/",pdfName]];

const char *cfilePath = [filePath UTF8String];

CFStringRef pathRef = CFStringCreateWithCString(NULL, cfilePath, kCFStringEncodingUTF8);

// 3.设置当前pdf页面的属性

CFStringRef myKeys[3];

CFTypeRef myValues[3];

myKeys[0] = kCGPDFContextMediaBox;

myValues[0] = (CFTypeRef) CFDataCreate(NULL,(const UInt8 *)&mediaBox, sizeof (CGRect));

myKeys[1] = kCGPDFContextTitle;

myValues[1] = CFSTR("我的PDF");

myKeys[2] = kCGPDFContextCreator;

myValues[2] = CFSTR("Name");

CFDictionaryRef pageDictionary = CFDictionaryCreate(NULL, (const void **) myKeys, (const void **) myValues, 3,

&kCFTypeDictionaryKeyCallBacks, & kCFTypeDictionaryValueCallBacks);

// 4.获取pdf绘图上下文

CGContextRef myPDFContext = MyPDFContextCreate (&mediaBox, pathRef);

// 5.开始描绘第一页页面

CGPDFContextBeginPage(myPDFContext, pageDictionary);

scrollV.contentOffset = CGPointZero;

//    CGRect s = scrollV.frame;

//    s.size = scrollV.contentSize;

//    UIGraphicsBeginImageContextWithOptions(s.size, YES, [UIScreen mainScreen].scale);

UIGraphicsBeginImageContext(CGSizeMake(scrollV.contentSize.width, scrollV.contentSize.height));

[scrollV.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage*image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

image = [self clipWithImageRect:CGRectMake(0, 0, image.size.width, image.size.height/2) clipImage:image];

//上面的图片 画图用数学坐标系

CGImageRef pageImage = [image CGImage];

CGContextDrawImage(myPDFContext, CGRectMake(0, scrollV.contentSize.height-image.size.height, [image size].width, [image size].height), pageImage);

scrollV.contentOffset = CGPointMake(0, scrollV.contentSize.height-image.size.height);

UIGraphicsBeginImageContext(scrollV.contentSize);

[scrollV.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage*image2 = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

image2 = [self clipWithImageRect:CGRectMake(0, scrollV.contentSize.height/2, image2.size.width, image2.size.height/2) clipImage:image2];

CGImageRef pageImage2 = [image2 CGImage];

CGContextDrawImage(myPDFContext, CGRectMake(0, 0, [image2 size].width, [image2 size].height), pageImage2);

CGPDFContextEndPage(myPDFContext);

CFRelease(pageDictionary);

CFRelease(myValues[0]);

CGContextRelease(myPDFContext);

scrollV.contentOffset = CGPointZero;

最后改变scrollview的偏移量

这个代码已经可以满足需求了,不过有局限性,就是如果pdf的页数超过了两页,就不能用了,于是我又花了好长的时间优化代码,被pdf搞的累死了。如果有问题随时可以跟我交流:QQ:872486713

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • UIGraphicsBeginImageContext 创建一个基于位图的上下文(context),并将其设置为当...
    李前途阅读 3,257评论 1 4
  • --绘图与滤镜全面解析 概述 在iOS中可以很容易的开发出绚丽的界面效果,一方面得益于成功系统的设计,另一方面得益...
    韩七夏阅读 2,773评论 2 10
  • 在通往知识的顶峰的路上长满了荆棘,望你不畏艰险、克服困难、勇往直前!
    鼗豸爻爻阅读 199评论 0 0
  • 文/宁静的烟火 青春的岁月就像是一条河,流着流着就变成浑汤了。 2017年9月7日 星期四 晴 我是个痞子。...
    宁静的烟火阅读 981评论 13 21
  • 重逢 文/冰百合 再也没有那样地重逢了 松林的光阴慢 慢慢地走过二十四节气 万水千山的苍翠停留在它的发梢 细雨...
    冰百合阅读 370评论 1 4