2010-08-23 47 views
0

我剛開始構建一個將顯示PDF文檔的應用程序。我一直在做實驗,繼承UIView並使用蘋果演示代碼。我有一個PDF文檔,其中包含的圖像在131 ppi時爲1024 x 748像素,因此它應該以橫向視圖填充iPad屏幕。CGPDFDocument scaling

當我運行該應用程序時,pdf縮放到其全尺寸的大約0.25%,集中在iPad屏幕上。爲什麼不顯示完整大小的PDF?從我自定義的UIView

代碼:

-(id)initWithFrame:(CGRect)frame PDFName:(NSString *)pdfName 
{ 
    self = [super initWithFrame:frame]; 
    if(self != nil) 
    { 
     self.backgroundColor = [UIColor blueColor]; 
     self.opaque = YES; 
     self.clearsContextBeforeDrawing = YES; 

     CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), (CFStringRef)pdfName, NULL, NULL); 
     pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL); 
     CFRelease(pdfURL); 
    } 

    return self; 
} 


- (void)drawRect:(CGRect)rect { 

    // PDF page drawing expects a Lower-Left coordinate system, so we flip the coordinate system 
    // before we start drawing. 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextTranslateCTM(context, 0.0, self.bounds.size.height); 
    CGContextScaleCTM(context, 1.0, -1.0); 

    // Grab the first PDF page 
    CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 1); 

    // We're about to modify the context CTM to draw the PDF page where we want it, so save the graphics state in case we want to do more drawing 
    CGContextSaveGState(context); 
    // CGPDFPageGetDrawingTransform provides an easy way to get the transform for a PDF page. It will scale down to fit, including any 
    // base rotations necessary to display the PDF page correctly. 
    CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, self.bounds, 0, true); 
    // And apply the transform. 
    CGContextConcatCTM(context, pdfTransform); 
    // Finally, we draw the page and restore the graphics state for further manipulations! 
    CGContextDrawPDFPage(context, page); 
    CGContextRestoreGState(context); 

} 

回答

1

答案很簡單。將PDF中圖像的ppi更改爲72 ppi(仍爲1024 x 748)。現在它正確地填充屏幕。我認爲我需要匹配iPad的像素密度,但我猜不是。

Jk

+0

PPI與它無關。機會是你的初始PDF分辨率是612×792的正常邊界框。改變圖像的邊界可以解決一個PDF的問題,但真正的答案在於改變PDF轉換的值,或拉伸/縮放pdf視圖或超級視圖。 – J2theC 2013-04-14 02:35:39