2010-03-15 36 views
8

Iam嘗試將UIImage保存爲PDF文件。 我該怎麼做?我如何保存和圖像到PDF文件,然後導出該PDF文件? 請爲我遇到的問題提出解決方案。將UIImage轉換爲PDF文件

謝謝。

+0

爲什麼?雖然有可能,但PDF已針對存儲文檔和矢量圖形進行了優化,而UIImage是一個位圖... – kennytm 2010-03-15 14:16:35

回答

3

我的理解是,你會創建一個CGPDFContext,繪製你的UIImage,並保存到一個文件。不過,我自己並沒有這樣做。

15

你好,我發現這個工程, 希望它有幫助!

-(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename 
    { 
     // Creates a mutable data object for updating with binary data, like a byte array 
     NSMutableData *pdfData = [NSMutableData data]; 

     // Points the pdf converter to the mutable data object and to the UIView to be converted 
     UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil); 
     UIGraphicsBeginPDFPage(); 

     // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData 
     [aView.layer renderInContext:UIGraphicsGetCurrentContext()]; 

     // remove PDF rendering context 
     UIGraphicsEndPDFContext(); 

     // Retrieves the document directories from the iOS device 
     NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); 

     NSString* documentDirectory = [documentDirectories objectAtIndex:0]; 
     NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename]; 

     // instructs the mutable data object to write its context to a file on disk 
     [pdfData writeToFile:documentDirectoryFilename atomically:YES]; 
     NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename); 
    } 
+0

似乎是一個很好的概念,但繪製了一個空白的PDF文件? 「aView」drawRect需要什麼? – WrightsCS 2010-12-17 02:44:45

+1

完全它的工作....比q sooooooooo多streeter amduser440071 – 2011-12-06 05:11:48

0

我也收到了一個空白pdf。儘管現在工作了。嘗試改變:

//[aView drawRect:aView.bounds]; // <- This 

[aView.layer renderInContext:UIGraphicsGetCurrentContext()]; // <- To This 
-1

可以啓動一個PDF圖形上下文,然後繪製的圖像到它,使用:

[UIImage drawInRect: someRect]; 

您可以看到文檔,他們給生成的一個很好的解釋PDF格式。 pdf生成有一個很好的教程here