2012-06-11 23 views
0

我要創造我用下面的代碼從瀏覽PDF我已經從網上得到了這一點,但如何調用該方法,並在那裏給的文件名創建PDF保存在文檔文件夾中。調用在iPhone應用程序中創建PDF方法也不工作

當我把這種方法它給發送異常無法識別的選擇。

[self createPDFfromUIView:someView saveToDcumentsWithFileName:@"my_pdf.pdf"]; 



-(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(); 
CGContextRef pdfContext = UIGraphicsGetCurrentContext(); 


// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData 

[aView.layer renderInContext:pdfContext]; 

// 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

。例如createPDFfromUIView,它將從您要創建PDF的UIview對象和saveToDocumentsWithFileName告訴您,傳遞您想要的pdf文件的名稱。該文件將在Document目錄中創建。 – Swapnil

+0

我這樣做給了異常,當我把這個方法說無法識別的選擇發送 – james

+0

@EXC_BAD_ACCESS請如何我打電話的方法是正確與否 – james

回答

0

這是工作代碼....不要忘記如果你仔細閱讀方法名提供你的一切添加QuartzCore.h

#include <QuartzCore/QuartzCore.h> 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 


    [self createPDFfromUIView:self.view saveToDocumentsWithFileName:@"abc.pdf"]; 

    } 
    -(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(); 
CGContextRef pdfContext = UIGraphicsGetCurrentContext(); 


// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData 


[aView.layer renderInContext:pdfContext]; 

// 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

如果你有在類中實現該方法,並從同一個類中調用它。你會做它像這樣:

[self createPDFfromUIView:someView saveToDcumentsWithFileName:@"my_pdf.pdf"]; 

這將節省您的UIView爲PDF在Documents目錄中的,my_pdf.pdf一個文件名。

希望這會有所幫助。

+0

調用此方法給出了異常terminationg應用由於無法識別的選擇發送到你行書面 – james

+0

我認爲我們需要首先創建文件 – james

+0

您必須將該方法放在類'@ implementation'中。無法識別的選擇器意味着你沒有從正確的地方調用它。 – skram

相關問題