2012-05-04 19 views
3

我有一個名爲UIView + PDFView的UIView擴展類,它接受當前視圖,將其拆分爲頁面並呈現PDF文檔。我的問題在於渲染部分;我可以成功地爲內容創建頁面,但是第一頁之後的所有頁面都是空白的。我的最終目標是採用當前視圖,「縮放」與頁面相同的寬度,並在PDF中對視圖的其餘部分進行分頁。Obj-C查看PDF只呈現第一頁

什麼問題不在於:

  • 附加的PDF以電子郵件
  • 的PDF發送到打印機/打印仿真
  • 只打印一頁,而實際上正確生成PDF
  • 正確調整我做這個觀點(SO Question 4919388)
    • 之前,我送方法。通過使幀高10px進行驗證,仍然打印一個(且僅一個)整頁
  • 正確翻譯視圖。通過翻譯和規模進行驗證;都正確地改變了視圖,但是在第一頁上都沒有呈現。

我的代碼如下:

@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,你可以直接使用UIGraphicsBeginPDFPage();

自定義頁面大小,您可以使用UIGraphicsBeginPDFPageWithInfo(CGRectMake(0.0, 0.0, 612.0, 792.0), nil);

我認爲應該解決您的問題。

+0

我使用了你的代碼,並在renderInContext:方法之前添加了下面的變換,並得到了我期待的結果,謝謝! 'CGContextTranslateCTM(pdfContext,0, - (pageSize.height * currentPage));' – sbonami

+1

謝謝@ScottBonAmi這正是我的問題! –