来自PDF的高品质UIImage [英] High quality UIImage from PDF

查看:135
本文介绍了来自PDF的高品质UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将PDF页面转换为UIImage。在这样做的同时,我失去了图像质量。需要帮助才能获得高质量的图像。

I'm converting a PDF page into a UIImage. While doing so, I lose the image quality. Need help in getting high quality images.

生成UIImage的代码

-(UIImage *)imageForPage:(int)pageNumber {
    CGPDFPageRef pdfPageRef = [self pdfReferenceForPage:pageNumber];

    CGSize pageSize = [self sizeOfPage:pageNumber];

    //UIGraphicsBeginImageContext(pageSize);

    UIGraphicsBeginImageContextWithOptions(pageSize, NO, 0.0);

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

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

    CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(pdfPageRef, kCGPDFCropBox, CGRectMake(0, 0, pageSize.width, pageSize.height), 0, true);
    CGContextConcatCTM(context, pdfTransform);

    CGContextDrawPDFPage(context, pdfPageRef);
    CGContextRestoreGState(context);

    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return resultingImage;
}


- (void)saveImage {
       UIImage *image = [self imageForPage:1];
       [UIImageJPEGRepresentation(image, 1.0) writeToFile:filePath atomically:YES];
}

图片比较

原始PDF

来自PDF的图像

编辑:代码更改

float dpi = 100.0 / 72.0;

CGPDFPageRef pdfPageRef = [self pdfReferenceForPage:pageNumber];

CGSize pageSize = [self sizeOfPage:pageNumber];
pageSize.width = pageSize.width * dpi;
pageSize.height = pageSize.height * dpi;

UIGraphicsBeginImageContext(pageSize);

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextTranslateCTM(context, 0.0, pageSize.height);
CGContextScaleCTM(context, dpi, -dpi);
CGContextSaveGState(context);
//CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(pdfPageRef, kCGPDFCropBox, CGRectMake(0, 0, pageSize.width, pageSize.height), 0, true);
//CGContextConcatCTM(context, pdfTransform);
CGContextDrawPDFPage(context, pdfPageRef);
CGContextRestoreGState(context);

UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return resultingImage;


推荐答案

当你询问PDF的页面大小时,你获得72 PPI的宽度/高度。您可以尝试创建使用缩放变换放大的上下文。例如,如果您想以300 dpi渲染,请按 300.0 / 72.0 添加比例变换。

When you ask the PDF for it's page size, you're getting a width/height for 72 PPI. You might try creating a context that's scaled up using a scale transform. For example, if you wanted to render at 300 dpi, add a scale transform to scale by 300.0/72.0.

如果导出为TIFF,则可以封装生成图像的最终PPI(300)。

If you export as TIFF, you will be able to encapsulate the final PPI (300) of the generated image.

这篇关于来自PDF的高品质UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆