2012-09-18 4 views
0

PDF를 경계 사각형으로 렌더링하는 사용자 정의 뷰를 만듭니다.
나는 코드 아래에 붙여 넣은 :drawRect 및 drawLayerInContext를 사용하여 iOS에서 PDF 렌더링

CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0); 
CGContextFillRect(context, self.bounds); 

CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox); 
CGFloat scale = MIN(self.bounds.size.width/pageRect.size.width, self.bounds.size.height/pageRect.size.height); 
CGContextSaveGState(context); 
CGContextTranslateCTM(context, 0.0, self.bounds.size.height); 
CGContextScaleCTM(context, 1.0, -1.0); 
CGContextScaleCTM(context, scale, scale);  
CGContextDrawPDFPage(context, self.page); 
CGContextRestoreGState(context); 

내 질문에 내가의 drawRect 방법에 위의 코드를 사용하는 경우 그냥 나에게 검은 사각형을 제공합니다. drawLayer : inContext : 메서드에서 사용하면 잘 작동합니다. 그 이유는 무엇일까요?

PS : drawRect를 사용하는 경우 UIGraphicsGetCurrentContext() 메서드를 사용하여 현재 그래픽 컨텍스트를 가져옵니다.

답변

3

이 코드는 실제로 나를 위해 잘 작동 .. 중단 점을 추가하고이 -drawRect 방법

CGContextRef context = UIGraphicsGetCurrentContext(); 
NSString *filePath = [[NSBundle mainBundle] 
         pathForResource:@"Trial" ofType:@"pdf"]; 
NSURL *url = [[NSURL alloc] initFileURLWithPath:filePath]; 
CGPDFDocumentRef document = [self openDocument:(CFURLRef)url]; 
[url release]; 

CGPDFPageRef docPageRef =CGPDFDocumentGetPage(document, 1); 

CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0); 
CGContextFillRect(context, self.bounds); 

CGRect pageRect = CGPDFPageGetBoxRect(docPageRef, kCGPDFMediaBox); 
CGFloat scale = MIN(self.bounds.size.width/pageRect.size.width, self.bounds.size.height/pageRect.size.height); 
CGContextSaveGState(context); 
CGContextTranslateCTM(context, 0.0, self.bounds.size.height); 
CGContextScaleCTM(context, 1.0, -1.0); 
CGContextScaleCTM(context, scale, scale); 
CGContextDrawPDFPage(context, docPageRef); 
CGContextRestoreGState(context); 

내의 drawRect이 사용하고 괜찮 았는데 안타 확인.

확인 모든 참조 당신이의 drawRect check this

+0

그것은 내 프로젝트와 관련된 몇 가지 문제에 제공하는 가치를 얻고있다. 그리고 왜 다른 하나의 복용량 동안 작동 하나 알아 내려고. –