最近項目中新增了一個分享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