2012-05-04 2 views
3

UIView + PDFView라는 확장 클래스가 있는데, 현재 뷰를 가져 와서 페이지로 분할하고 PDF 문서를 렌더링합니다. 내 문제는 렌더링 부분에 있습니다. 나는 내용을위한 페이지를 성공적으로 '창조 할 수있다, 그러나 첫번째 페이지 다음에 모든 페이지는 공백이다. 최종 목표는 현재 뷰를 가져 와서 페이지와 동일한 너비의 '비율'을 조정하고 PDF 내에서 나머지 뷰의 페이지를 매기는 것입니다.Obj-C PDF보기는 첫 번째 페이지 만 렌더링합니다.

는 문제가되지 않습니다 무엇 :

  • 가 이메일을하는
  • 을 실제로 제대로
  • 을 PDF를 생성하는 동안
  • 만 한 페이지를 인쇄 할 프린터/인쇄 시뮬레이터에 PDF 전송 PDF를 부착
  • 보기의 크기를 올바르게 조정하십시오. (SO Question 4919388)
    • 방법으로 보내기 전에이 작업을 수행합니다. 프레임을 10 픽셀 높이로 확인하여 한 페이지 만 인쇄합니다.
  • 보기를 올바르게 번역합니다. 번역과 스케일 모두를 확인함으로써; 둘 다 올바르게보기를 변경했지만 첫 페이지 이상으로 렌더링되지 않았습니다. 다음과 같이

내 코드는 다음과 같습니다

@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 이것이 정확하게 제 문제였습니다! –

관련 문제