2015-10-06 2 views
6

私はこのコードを使って、私のUIViewを使ってPDFに変換します。私の問題は、私のビューはディテールビューコントローラーでスクロール可能で、PDFはその時点でディテールビューコントローラー内のものだけを取得し、フルビューではないということです。詳細ビューで移動すると、私は全部ではなく、一部です。私は何をしようとしていますか?Objective-CフルUIViewからPDFへ

- (IBAction)Share:(id)sender { 

    NSMutableData *pdfData = [NSMutableData data]; 

    // Points the pdf converter to the mutable data object and to the UIView to be converted 
    UIGraphicsBeginPDFContextToData(pdfData, spreadSheet.bounds, nil); 
    UIGraphicsBeginPDFPage(); 
    CGContextRef pdfContext = UIGraphicsGetCurrentContext(); 


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

    [spreadSheet.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:@"test.pdf"]; 

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


    UIActivityViewController * activityController = [[UIActivityViewController alloc] initWithActivityItems:@[pdfData] applicationActivities:nil]; 

    UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:activityController]; 

    [popup presentPopoverFromRect:CGRectMake(self.view.frame.size.width - 36, 60, 0, 0)inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES]; 

} 

UPDATE

私は本当にこれで苦労してきた、私はPDFへの完全なビューを得ることができない、私はまた私のNSMutableArrayのと私にNSArrayを取るのルートに行くことを考えていますこれは私のUIViewで使用されるデータであり、PDFに変換しようとしていますが、誰かがそれを行う方法を知らない限り、フォーマットをうまくやっていくのに時間がかかるようです。私はどちらのルートを取るべきか混乱していると思う。

+0

実際には「完全なUIView」ではなく、さまざまな状態の1つのUIViewを編集していますか?あなたの詳細ビューコントローラには何を使用していますか?どのクラス? – Tommy

+0

境界の代わりにスプレッドシートのコンテンツサイズを使用しようとしましたか? – Sheamus

+0

"spreadSheet"の階層は何ですか?それが見えないセルがまだレンダリングされていないので、これが単純な方法ではなく、テーブルビューまたはコレクションビューである場合。 – Aris

答えて

1

コンテキストに何かをレンダリングするのは、現在ビュー階層内にあるものだけをレンダリングすることです。つまり、UITableViewまたはUICollectionViewを使用している場合は、データを表すすべてのセルが任意の時点で階層内にあるわけではありません。それが私だったら、一時的にビューを設定して、すべてが階層構造になるように大規模なフレームを作成しようとします。それがうまくいかない場合は、要求に応じてすべてのデータのすべてをレイアウトし、レンダリングが完了した後に効率的なレイアウトにリセットできるカスタムビューを作成します。

0

UIViewを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); 
} 

また、インポートしてください:QuartzCore/QuartzCore.h

2

この

から私が午前私の問題は、私の見解は、詳細ビューコントローラであると がスクロール可能で、PDFだけで何を得るということですその時点で詳細ビュー コントローラの中にあり、完全なビューではありません。

表示されているコンテンツはスクロール可能で、作成したPDFは表示領域のみをキャプチャするという問題があります。

ここにあなたの問題

UIGraphicsBeginPDFContextToData(pdfData、spreadSheet.bounds、nilの)です。

ここで、スクロール可能なビュー(UITableView、UICollectionViewまたはScrollview)を与える必要がある場合は、修正する必要があります。コンテンツサイズの範囲はここで行います。

-(NSData*)pdfDataFromTableViewContent 
{ 
    NSMutableData *pdfData = [NSMutableData data]; 
    //The real size, not only the visible part use your scrollable view ((UITableView,UICollectionView or Scrollview)) 
    CGSize contentSize = self.tableView.contentSize; 
    CGRect tableViewRealFrame = self.tableView.frame; 
    //Size tableView to show the whole content 
    self.tableView.frame = CGRectMake(0, 0, contentSize.width, contentSize.height); 
    //Generate PDF (one page) 
    UIGraphicsBeginPDFContextToData(pdfData,self.tableView.frame, nil); 
    UIGraphicsBeginPDFPage(); 
    [self.tableView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIGraphicsEndPDFContext(); 
    //Resize frame 
    self.tableView.frame = tableViewRealFrame; 
    return pdfData; 
} 

上記のコードは1ページを生成します。より多くのページについては はあなたにもこの作業をプロジェクトScrollViewToPDFに見ることができ、コード

//MultiPage 
CGRect mediaBox = self.tableView.frame; 
CGSize pageSize = CGSizeMake(self.view.frame.size.width, 800); 
UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil); 
CGContextRef pdfContext = UIGraphicsGetCurrentContext(); 
NSInteger currentPage = 0; 
BOOL done = NO; 
do { 
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0.0, pageSize.width, pageSize.height), nil); 
    CGContextTranslateCTM(pdfContext, 0, -(pageSize.height*currentPage)); 
    [self.view.layer renderInContext:pdfContext]; 
    if ((pageSize.height*(currentPage+1)) > mediaBox.size.height) done = YES; 
    else currentPage++; 
} while (!done); 
UIGraphicsEndPDFContext(); 

次を使用します。 同じスクロールビューのレイヤーrenderInContextを使用しますが、ここではPDFは1ページPDFや複数ページPDFなどの要件に従って作成されます。 scrollViewのすべての可視部分と不可視部分をキャプチャします

+0

これをSwiftに置くことができますか?ありがとう! – Derry