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,258評論 1 4
  • --繪圖與濾鏡全面解析 概述 在iOS中可以很容易的開發出絢麗的界面效果,一方面得益于成功系統的設計,另一方面得益...
    韓七夏閱讀 2,796評論 2 10
  • 在通往知識的頂峰的路上長滿了荊棘,望你不畏艱險、克服困難、勇往直前!
    鼗豸爻爻閱讀 201評論 0 0
  • 文/寧靜的煙火 青春的歲月就像是一條河,流著流著就變成渾湯了。 2017年9月7日 星期四 晴 我是個痞子。...
    寧靜的煙火閱讀 986評論 13 21
  • 重逢 文/冰百合 再也沒有那樣地重逢了 松林的光陰慢 慢慢地走過二十四節氣 萬水千山的蒼翠停留在它的發梢 細雨...
    冰百合閱讀 371評論 1 4