2011-06-21 54 views
1

我想基本上了解如何創建一個簡單的PDF,並轉化爲iPhone上的UIImage。我曾經拼湊驗證碼:如何繪製PDF NSData到UIImage?

NSMutableData *pdfData = [[NSMutableData alloc] init]; 
UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil); 

CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, (CFStringRef)@"Text", NULL); 
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText); 
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil); 
CGMutablePathRef framePath = CGPathCreateMutable(); 
CGPathAddRect(framePath, NULL, CGRectMake(0, 0, 612, 792)); 
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, 
              CFRangeMake(0, [(NSString *)currentText length]), 
              framePath, 
              NULL); 

CTFrameDraw(frame, UIGraphicsGetCurrentContext()); 
UIGraphicsEndPDFContext(); 
UIImage *pdfImage = [UIImage imageWithData:pdfData] 

我相當肯定,這個代碼是正確的,除了最後一行(因爲UIImage的無法閱讀PDF數據是不正確的),所以我試圖找出哪些我可以將我的PDF發送到課程。

'pdfData'不是零,它大小約爲5000字節。

任何幫助你們可以給予非常感謝。乾杯。

+0

傳遞給'UIGraphicsBeginPDFContextToData()'的邊界不應該是'CGRectZero'嗎? –

+0

將CGRectZero賦予此函數可爲上下文提供(0,0,612,792)的默認邊界。 – Dyldo42

回答

2

您可以使用UIWebView來閱讀pdf或DocumentPreviewController。

+0

它的工作原理! UIWebView是正確的。我需要翻轉座標才能正確顯示文本,但這是另一回事。 如果有人想知道,這是我用來將我的PDF在webview中的代碼,我初始化後: [pdfWebView loadData:pdfData MIMEType:@「application/pdf」textEncodingName:@「utf-8 「baseURL:[NSURL URLWithString:@」「]]; – Dyldo42