2011-05-26 45 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