2014-01-23 35 views
0

我試圖從PDF中捕獲圖像並在UIScrollView的iPad應用程序中顯示它們。當pdf頁面大小爲1024 x 768(標準4:3寬高比)時,應用程序將圖像渲染爲全屏(全尺寸)。但是,當PDF頁面大小爲720 x 540(相同的4:3縱橫比,但尺寸較小)時,我看到圖像周圍出現白色邊框。以下是我試圖捕獲圖像的代碼片段。較小頁面尺寸的圖片PDF在iOS6中沒有自動實現全屏顯示

CGPDFPageRef thePDFPageRef = CGPDFDocumentGetPage(pdfDocRef, pdfIterator); 
CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB(); // RGB color space 

     CGBitmapInfo bmi = (kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast); 
     thumbnailWidth = 1024; 
     thumbnailHeight = 768; 
     CGContextRef context = CGBitmapContextCreate(NULL, thumbnailWidth, thumbnailHeight, 8, 0, rgb, bmi); 


     if (context != NULL) // Must have a valid custom CGBitmap context to draw into 
     { 
      CGRect thumbRect = CGRectMake(0.0f, 0.0f, thumbnailWidth, thumbnailHeight); // Target thumb rect 

      CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f); 

      CGContextFillRect(context, thumbRect); // White fill 

      CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(thePDFPageRef, kCGPDFCropBox, thumbRect, 0, true)); // Fit rect 

      CGContextSetRenderingIntent(context, kCGRenderingIntentDefault); 
      CGContextSetInterpolationQuality(context, kCGInterpolationHigh); 

      CGContextDrawPDFPage(context, thePDFPageRef); // Render the PDF page into the custom CGBitmap context 

      imageRef = CGBitmapContextCreateImage(context); // Create CGImage from custom CGBitmap context 

      CGContextRelease(context); // Release custom CGBitmap context reference 
     } 

     CGColorSpaceRelease(rgb); // Release device RGB color space reference 

     CGPDFPageRelease(thePDFPageRef); 
    } 


    UIImage *image = [UIImage imageWithCGImage:imageRef scale:scale orientation:0]; 

任何人都可以幫助我如何自動縮放到全屏,即使PDF頁面尺寸較小。

回答

0

我發現解決方案如下縮放矩形。希望這有助於一些身體其他..

(1)使用Get PDF頁的矩形

CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox); 

(2)做縮放矩形,並應用變換單位矩陣

CGAffineTransform trans = CGAffineTransformIdentity; 
trans = CGAffineTransformTranslate(trans, 0, pageRect.size.height); 
trans = CGAffineTransformScale(trans, 1.0, -1.0); 
rect = CGRectApplyAffineTransform(rect, trans); 

(3)使用kCGPDFMediaBox而不是kCGPDFCropBox來避免在大小與標準不同時裁剪pdf。

感謝, 西瓦

相關問題