2012-05-04 15 views
3

UIView + PDFViewと呼ばれるUIView拡張クラスがあり、現在のビューをページに分割してPDFドキュメントをレンダリングします。私の問題はレンダリング部分にあります。私はコンテンツのページを「作成」することはできますが、最初のページ以降のすべてのページは空白です。私の最終的な目標は、現在のビューを取得し、ページの幅を「拡大縮小」し、PDF内のビューの残りのページをページングすることです。問題がないとは何Obj-C PDFを表示すると、最初のページがレンダリングされます

:電子メールにPDFの添付

  • 実際に正しく
  • をPDFの生成中に一つだけのページを印刷するプリンタ/プリントシミュレータへ
  • をPDFを送信
  • ビューのサイズを正しく変更する(SO Question 4919388)
    • 私はメソッドに送信する前にこれを行います。フレーム10pxを高くしても、1ページ(1ページのみ)のフルページが印刷されます。
  • ビューを正しく翻訳しています。翻訳と縮尺の両方を確認することによって確認されます。どちらもビューを正しく変更しましたが、最初のページ以上には表示されませんでした。

私のコードは次のとおりです。

@interface UIView (PDFView) 
-(id)createPDFAndSaveToDocumentsWithFileName:(NSString*)aFilename andDocumentInfo:(NSDictionary *)documentInfo; 
@end 

@implementation UIView (PDFView) 

-(id)createPDFAndSaveToDocumentsWithFileName:(NSString*)aFilename andDocumentInfo:(NSDictionary *)documentInfo { 
    // http://developer.apple.com/library/ios/DOCUMENTATION/GraphicsImaging/Reference/CGPDFContext/Reference/reference.html#//apple_ref/doc/constant_group/Auxiliary_Dictionary_Keys 

    // 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 
    CGSize pageSize = CGSizeMake(self.bounds.size.width, 792); 
    UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, documentInfo); 
    CGContextRef pdfContext = UIGraphicsGetCurrentContext(); 

    NSInteger currentPage = 0; 
    BOOL done = NO; 

    do { 
     CGRect currentPageRect = CGRectMake(0, (pageSize.height*currentPage), pageSize.width, pageSize.height); 
     UIGraphicsBeginPDFPageWithInfo(currentPageRect, nil); 

     // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData 
     [self.layer renderInContext:pdfContext]; 

     // If we're at the end of the view, exit the loop. 
     if ((pageSize.height*currentPage) > self.bounds.size.height) { 
      done = YES; 
     } else { 
      currentPage++; 
     } 
    } while (!done); 

    // remove PDF rendering context 
    UIGraphicsEndPDFContext(); 

    if (aFilename == nil) { 
     return pdfData; 
    } else { 
     // 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); 
     return nil; 
    } 
} 

答えて

4

私はあなたの問題ではなく、その

CGRect currentPageRect = CGRectMake(0, (pageSize.height*currentPage), pageSize.width, pageSize.height); 

あなたがいずれかの以下の文

UIGraphicsBeginPDFPageWithInfo(CGRectMake(0.0, 0.0, 612.0, 792.0), nil); 
UIGraphicsBeginPDFPage(); 

のたびを使用してみてくださいあると思いますコンテキストに新しいページを追加したい場合は上記のステートメントを使用し、コンテキストにページを追加することができます。ページのデフォルトサイズを使用したい場合は

すなわち612 X 792あなたは直接、私はそれがあなたの問題を解決すべきだと思うあなたはUIGraphicsBeginPDFPageWithInfo(CGRectMake(0.0, 0.0, 612.0, 792.0), nil);

を使用することができますカスタムページサイズのためUIGraphicsBeginPDFPage();

を使用することができます。

+0

私はあなたのコードを使用し、renderInContext:メソッドの前に以下のトランスフォームを追加し、私が探している結果を得ました! 'CGContextTranslateCTM(pdfContext、0、 - (pageSize.height * currentPage)); ' – sbonami

+1

ありがとう@ScottBonAmiこれはまさに私の問題でした! –

関連する問題