2009-08-27 2 views
2

PDF에서 나온 NSImage가 있으므로 NSPDFImageRep 유형의 표현이 하나 있습니다. 나는 이미지를한다 setDataRetained : YES; 그것이 NSPDFImageRep로 남아 있는지 확인하십시오. 나중에, 나는 페이지를 바꾸고 싶다. 그래서 나는 대표를 얻고 현재 페이지를 설정한다. 이건 괜찮아.NSImageRep confusion

문제는 이미지를 그릴 때 첫 번째 페이지 만 나오게된다는 것입니다.

제 생각에는 NSImage를 그릴 때 표현을 선택하고 그 표현을 그립니다. 이제 이미지에는 한 명의 담당자 만 있으므로 그 사람이 그려지는 사람이고 그 사람은 PDFrep입니다. 그래서, 왜 이미지를 그릴 때 올바른 페이지를 그리지 않는 것입니까?

그러나 표현 자체를 그릴 때 올바른 페이지가 나타납니다.

무엇이 누락 되었습니까?

답변

1

NSImage는 처음 표시 될 때 NSImageRep의 캐싱을 수행합니다. NSPDFImageRep의 경우 "setCacheMode :"메시지는 아무 효과가 없습니다. 따라서, 표시 될 페이지는 항상 첫 번째 페이지가됩니다. 자세한 내용은 this guide을 참조하십시오. 직접 표현을 그리기

  1. :

    당신은 다음 두 가지 솔루션이있다.

  2. NSImage에서 "recache"메시지를 호출하여 선택한 페이지의 래스터 화를 강제 실행하십시오.
1

PDF를 그리는 또 다른 메커니즘은 CGPDF * 기능을 사용하는 것입니다. 이렇게하려면 CGPDFDocumentCreateWithURL을 사용하여 CGPDFDocumentRef 개체를 만듭니다. 그런 다음 CGPDFDocumentGetPage을 사용하여 CGPDFPageRef 개체를 가져옵니다. 그런 다음 CGContextDrawPDFPage을 사용하여 그래픽 컨텍스트에 페이지를 그릴 수 있습니다.

변환을 적용하여 문서의 크기가 원하는대로 끝나야 할 수도 있습니다. 이 작업을 수행하려면 CGAffineTransformCGContextConcatCTM을 사용하십시오.

// use your own constants here 
NSString *path = @"/path/to/my.pdf"; 
NSUInteger pageNumber = 14; 
CGSize size = [self frame].size; 

// if we're drawing into an NSView, then we need to get the current graphics context 
CGContextRef context = (CGContextRef)([[NSGraphicsContext currentContext] graphicsPort]); 

CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)path, kCFURLPOSIXPathStyle, NO); 
CGPDFDocumentRef document = CGPDFDocumentCreateWithURL(url); 
CGPDFPageRef page = CGPDFDocumentGetPage(document, pageNumber); 

// in my case, I wanted the PDF page to fill in the view 
// so we apply a scaling transform to fir the page into the view 
double ratio = size.width/CGPDFPageGetBoxRect(page, kCGPDFTrimBox).size.width; 
CGAffineTransform transform = CGAffineTransformMakeScale(ratio, ratio); 
CGContextConcatCTM(context, transform); 

// now we draw the PDF into the context 
CGContextDrawPDFPage(context, page); 

// don't forget memory management! 
CGPDFDocumentRelease(document); 
: 여기

는 일부 샘플 코드는 내 프로젝트 중 하나 뽑아입니다