【问题标题】:Get wrong thumbnail of PDF using imageFromPDFWithDocumentRef使用 imageFromPDFWithDocumentRef 获取错误的 PDF 缩略图
【发布时间】:2013-09-10 11:09:00
【问题描述】:

我在使用 imageFromPDFWithDocumentRef 获取我的 PDF 封面时遇到了一个奇怪的问题。

代码如下。

 - (UIImage *)imageFromPDFWithDocumentRef:(CGPDFDocumentRef)documentRef
{
    CGPDFPageRef pageRef = CGPDFDocumentGetPage(documentRef, 1);
    CGRect pageRect = CGPDFPageGetBoxRect(pageRef, kCGPDFCropBox);

    UIGraphicsBeginImageContext(pageRect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, CGRectGetMinX(pageRect),CGRectGetMaxY(pageRect));
    CGContextScaleCTM(context, 1, -1);  
    CGContextTranslateCTM(context, -(pageRect.origin.x), -(pageRect.origin.y));

    CGContextSetInterpolationQuality(context, kCGInterpolationLow);
    CGContextSetRenderingIntent(context, kCGRenderingIntentDefault);

    CGContextDrawPDFPage(context, pageRef);

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

    return finalImage;
}

但有时结果是这样的(“ABC”代表pdf文件名)

我本来就是这样的。

我想知道是否有人可以帮我一把,在此先感谢。 :)

【问题讨论】:

    标签: ios objective-c pdf pdf-generation


    【解决方案1】:

    您的代码不处理页面旋转,这就是在某些情况下输出旋转的原因。 替换此代码:

    CGContextTranslateCTM(context, CGRectGetMinX(pageRect),CGRectGetMaxY(pageRect));
    CGContextScaleCTM(context, 1, -1);  
    CGContextTranslateCTM(context, -(pageRect.origin.x), -(pageRect.origin.y));
    

    使用此代码:

    switch (rotate) {
        case 0:
            // Translate the origin of the coordinate system at the 
            // bottom left corner of the page rectangle.
            CGContextTranslateCTM(context, 0, cropBox.size.height);
            // Reverse the Y axis to grow from bottom to top.
            CGContextScaleCTM(context, 1, -1);
            break;
        case 90:
            // Reverse the Y axis to grow from bottom to top.
            CGContextScaleCTM(context, 1, -1);
            // Rotate the coordinate system.
            CGContextRotateCTM(context, -M_PI / 2);
            break;
        case 180:
        case -180:
            // Reverse the Y axis to grow from bottom to top.
            CGContextScaleCTM(context, 1, -1);
            // Translate the origin of the coordinate system at the 
            // top right corner of the page rectangle.
            CGContextTranslateCTM(context, cropBox.size.width, 0);
            // Rotate the coordinate system with 180 degrees.
            CGContextRotateCTM(context, M_PI);
            break;
        case 270:
        case -90:
            // Translate the origin of the coordinate system at the 
            // bottom right corner of the page rectangle.
            CGContextTranslateCTM(context, cropBox.size.height, cropBox.size.width);
            // Rotate the coordinate system.
            CGContextRotateCTM(context, M_PI / 2);
            // Reverse the X axis.
            CGContextScaleCTM(context, -1, 1);
            break;
    }
    

    在我的代码中,cropBox 与代码中的 pageRect 相同。关于 PDF 坐标系如何映射到图像/屏幕坐标系的更详细说明见我博客上的这篇文章(此代码取自那里):http://ipdfdev.com/2011/03/23/display-a-pdf-page-on-the-iphone-and-ipad/

    【讨论】:

    • 这真是及时的帮助!你能告诉我如何在你的代码中获得旋转吗?
    • int rotate = CGPDFPageGetRotationAngle(pageRef);
    • 我很难描述你对我的帮助有多大。非常感谢,完美解决了!
    猜你喜欢
    • 2011-07-03
    • 1970-01-01
    • 2014-01-22
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    • 2018-07-25
    • 2020-05-13
    • 2017-11-16
    相关资源
    最近更新 更多