2011-05-26 53 views
1

我有一種方法可以從CGPDFPageRef返回UIImage。您可以指定圖像的寬度。當縮放> 1時渲染PDF時出現白色邊框

問題是,當pdfScale> 1時,圖像中出現白色邊框。因此,PDF總是以比例1繪製,而不是更大的比例。較小的比例是可以的。

我試圖改變PDFBox的類型,但似乎並沒有改變任何東西,文件不是很清楚。

有人看到錯誤嗎?

- (UIImage*) PDFImageForWidth:(CGFloat) width { 
    CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFCropBox); 
    CGFloat pdfScale = width/pageRect.size.width; 
    pageRect.size = CGSizeMake(pageRect.size.width*pdfScale, pageRect.size.height*pdfScale); 
    pageRect.origin = CGPointZero; 

    UIGraphicsBeginImageContext(pageRect.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0); 
    CGContextFillRect(context, pageRect); 

    CGContextTranslateCTM(context, 0.0, pageRect.size.height); 
    CGContextScaleCTM(context, 1.0, -1.0); 
    CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, pageRect, 0, true)); 

    CGContextDrawPDFPage(context, page); 
    UIImage* image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return image; 
} 

回答

2

問題在於,當scale> 1時,CGPDFPageGetDrawingTransform不能縮放PDF。您必須自己動手。

此外,該方法現在是線程安全的。

- (UIImage*) PDFImageForWidth:(CGFloat) width { 
    CGRect smallPageRect = CGPDFPageGetBoxRect(page, kCGPDFCropBox); 
    CGFloat pdfScale = width/smallPageRect.size.width; 

    CGRect pageRect; 
    pageRect.size = CGSizeMake(smallPageRect.size.width*pdfScale, smallPageRect.size.height*pdfScale); 
    pageRect.origin = CGPointZero; 

    __block CGContextRef context; 
    dispatch_sync(dispatch_get_main_queue(), ^{ 
     UIGraphicsBeginImageContext(pageRect.size); 
     context = UIGraphicsGetCurrentContext(); 
    }); 

    if (context != nil) { 
     CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0); 
     CGContextFillRect(context, pageRect); 

     CGContextTranslateCTM(context, 0.0, pageRect.size.height); 
     CGContextScaleCTM(context, 1.0, -1.0); 

     CGAffineTransform transform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, pageRect, 0, true); 
     if (pdfScale > 1) { 
      transform = CGAffineTransformScale(transform, pdfScale, pdfScale); 
      transform.tx = 0; 
      transform.ty = 0; 
     } 
     CGContextConcatCTM(context, transform); 

     CGContextDrawPDFPage(context, page); 

     __block UIImage* image; 
     dispatch_sync(dispatch_get_main_queue(), ^{ 
      image = [UIGraphicsGetImageFromCurrentImageContext() retain]; 
      UIGraphicsEndImageContext(); 
     }); 

     return [image autorelease]; 
    } 
    else return nil; 
} 
+0

謝謝。如果這個限制在文檔中,那肯定會很好。 – Oded 2017-10-03 21:06:36

0

我有同樣的問題,以前的答案在某些地方幫助我,但它並沒有解決整個問題。

這是我如何解決我的問題。

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context 
{ 
    NSLog(@"pdfQuartzViewViewController - drawLayer"); 

    CGPDFDocumentRef myDocumentRef = CGPDFDocumentCreateWithURL((CFURLRef)[NSURL fileURLWithPath:self.strFilename]); 
    CGPDFPageRef pageReference = CGPDFDocumentGetPage(myDocumentRef, 1); 

    CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f); // White 
    CGContextFillRect(context, CGContextGetClipBoundingBox(context)); // Fill 

    CGContextTranslateCTM(context, 0.0f, layer.bounds.size.height); 

    // Scaling the pdf page a little big bigger than the view so we can cover the white borders on the page 
    CGContextScaleCTM(context, 1.0015f, -1.0015f); 
    CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(pageReference, kCGPDFCropBox, layer.bounds, 0, true); 
    pdfTransform.tx = pdfTransform.tx - 0.25f; 
    pdfTransform.ty = pdfTransform.ty - 0.40f; 

    CGContextConcatCTM(context, pdfTransform); 

    CGContextDrawPDFPage(context, pageReference); // Render the PDF page into the context 

    CGPDFDocumentRelease(myDocumentRef); 
    myDocumentRef = NULL; 
} 

這裏有一個很好的鏈接,你可以找到更多關於它的信息。這篇文章解釋瞭如何使用所有這些方法。

http://iphonedevelopment.blogspot.ch/2008/10/demystifying-cgaffinetransform.html