2014-01-07 29 views
0

有没有办法将PDF页面渲染到位图上下文中而不会丢失质量?在位图上下文中呈现的CFGPDF质量差

这里是我的代码:

-(UIImage *)imageForPage:(int)pageNumber sized:(CGSize)size{ 

    CGPDFDocumentRef _document = [self CreatePDFDocumentRef:filePath]; 
    CGPDFPageRef page = CGPDFDocumentGetPage (_document, pageNumber); 


    CGRect pageRect = CGPDFPageGetBoxRect(page,kCGPDFTrimBox); 
    CGSize pageSize = pageRect.size; 
    pageSize = MEDSizeScaleAspectFit(pageSize, size); 

    UIGraphicsBeginImageContextWithOptions(pageSize, NO, 0.0); 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh); 
    CGContextSetRenderingIntent(context, kCGRenderingIntentDefault); 

    CGContextTranslateCTM(context, 0.0, pageSize.height); 
    CGContextScaleCTM(context, 1.0, -1.0); 
    CGContextSaveGState(context); 

    CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFTrimBox, CGRectMake(0, 0, pageSize.width, pageSize.height), 0, true); 
    CGContextConcatCTM(context, pdfTransform); 
    CGContextDrawPDFPage(context, page); 
    CGContextRestoreGState(context); 

    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    CGPDFDocumentRelease (_document); 
    return resultingImage; 
} 


CGSize MEDSizeScaleAspectFit(CGSize size, CGSize maxSize) { 
    CGFloat originalAspectRatio = size.width/size.height; 
    CGFloat maxAspectRatio = maxSize.width/maxSize.height; 
    CGSize newSize = maxSize; 
    // The largest dimension will be the `maxSize`, and then we need to scale 
    // the other dimension down relative to it, while maintaining the aspect 
    // ratio. 
    if (originalAspectRatio > maxAspectRatio) { 
     newSize.height = maxSize.width/originalAspectRatio; 
    } else { 
     newSize.width = maxSize.height * originalAspectRatio; 
    } 

    return newSize; 
} 

Rendered Page

+3

你传递给这个函数的大小是多少?你在屏幕上显示的大小是多少?这些是我发现的图像清晰度中最重要的两个因素(如果您期望获得比您更好的图像质量)。 – Putz1103

+0

继Putz1103之后,如果您可以评论您认为图像质量有问题,那么这将有所帮助。你作为一个例子发布的内容怎么能被改进? – Tommy

+0

关于质量,与原始pdf文件相比,文本是模糊的。 关于尺寸,我改变了生成的图像的大小,以适应其目的地(1024×600),我可以看到主要的改善。现在看起来很棒。 –

回答

0

确保您所创建的图像是接近等于它会显示大小。这将创建一个视觉上最准确显示的图像。

+0

它的效果很好。谢谢。 –