来自PDF的高品质UIImage

时间:2013-01-04 06:50:46

标签: iphone objective-c

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

生成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 Original PDF


来自PDF的图片 Image from 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;

6 个答案:

答案 0 :(得分:5)

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

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

答案 1 :(得分:2)

试试Swift 3的代码:

func drawPDFfromURL(url: URL) -> UIImage? {
    guard let document = CGPDFDocument(url as CFURL) else { return nil }
    guard let page = document.page(at: 1) else { return nil }

    let pageRect = page.getBoxRect(.mediaBox)
    let renderer = UIGraphicsImageRenderer(size: pageRect.size)
    let img1 = renderer.jpegData(withCompressionQuality: 1.0, actions: { cnv in
        UIColor.white.set()
        cnv.fill(pageRect)
        cnv.cgContext.translateBy(x: 0.0, y: pageRect.size.height);
        cnv.cgContext.scaleBy(x: 1.0, y: -1.0);
        cnv.cgContext.drawPDFPage(page);
    })
    let img2 = UIImage(data: img1)
    return img2
}

这提供了更高的分辨率

答案 2 :(得分:2)

这个产品质量最好......如果你需要另一种品质,只需改变dpi。

    func drawPDFfromURL(url: URL) -> UIImage? {
    guard let document = CGPDFDocument(url as CFURL) else { return nil }
    guard let page = document.page(at: 1) else { return nil }
    let dpi: CGFloat = 300.0 / 72.0
    let pageRect = page.getBoxRect(.mediaBox)

    let renderer = UIGraphicsImageRenderer(size: CGSize(width: pageRect.size.width * dpi, height: pageRect.size.height * dpi))

    let img1 = renderer.jpegData(withCompressionQuality: 1.0, actions: { cnv in
          UIColor.white.set()
          cnv.fill(pageRect)
          cnv.cgContext.translateBy(x: 0.0, y: pageRect.size.height * dpi);
          cnv.cgContext.scaleBy(x: dpi, y: -dpi);
          cnv.cgContext.drawPDFPage(page);

        })
    let img2 = UIImage(data: img1)
    return img2
}

答案 3 :(得分:1)

我写了一个方便的UIImage PDF渲染器类别,你可能会觉得有用。它还会缓存所有渲染结果以获得高性能。

https://github.com/mindbrix/UIImage-PDF

答案 4 :(得分:0)

Class="container size" - 5*5,5*10 kind of value
Class="description" - Standard in this case also need to copy like Drive-up Access
Class="offer1" & "offer2" - Call for Availability
Class="price"

答案 5 :(得分:0)

从iOS 11.0,macOS 10.13,Mac Catalyst 13.0开始,您可以使用thumbnailOfSize:forBox: