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