最近項(xiàng)目中新增了一個(gè)分享pdf文件的的功能讲冠,把一個(gè)界面以pdf文件的形式分享出去券膀,剛開始接觸pdf辟躏,沒一點(diǎn)思路谷扣,各種查資料,中間踩了好多坑捎琐。下面我就把自己踩到的坑跟大家分享一下会涎。
??? 第一步要做的就是把uiview轉(zhuǎn)換成image,然后把image畫到pdf的畫布上野哭。
1 uiview轉(zhuǎn)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];
由于圖片不是全部都需要在塔,這里我進(jìn)行了一步切割處理
- (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文件上
//上面的圖片 畫圖用數(shù)學(xué)坐標(biāo)系
CGImageRef pageImage = [image CGImage];
CGContextDrawImage(myPDFContext, CGRectMake(0, scrollV.contentSize.height-image.size.height, [image size].width, [image size].height), pageImage);
由于我的需求比較特殊,這個(gè)view的底層是scrollview拨黔,所以這個(gè)scrollview的滾動(dòng)區(qū)域是動(dòng)態(tài)的蛔溃,不是一個(gè)屏幕大小這樣直接截個(gè)屏幕就行,于是我實(shí)驗(yàn)了好久發(fā)現(xiàn)篱蝇,改變scrollview的偏移量在截張然后把這兩張剪切一下拼起來就好了贺待。pdf從創(chuàng)建到結(jié)束的代碼如下:
// 1.創(chuàng)建media box
CGFloat myPageWidth = scrollV.contentSize.width;
CGFloat myPageHeight = scrollV.contentSize.height;
CGRect mediaBox = CGRectMake (0, 0, myPageWidth, myPageHeight);
// 2.設(shè)置pdf文檔存儲(chǔ)的路徑
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.設(shè)置當(dāng)前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];
//上面的圖片 畫圖用數(shù)學(xué)坐標(biāo)系
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的偏移量
這個(gè)代碼已經(jīng)可以滿足需求了,不過有局限性零截,就是如果pdf的頁數(shù)超過了兩頁麸塞,就不能用了,于是我又花了好長的時(shí)間優(yōu)化代碼涧衙,被pdf搞的累死了哪工。如果有問題隨時(shí)可以跟我交流:QQ:872486713