Ios 使用imageFromPDFWithDocumentRef获取错误的PDF缩略图

Ios 使用imageFromPDFWithDocumentRef获取错误的PDF缩略图,ios,objective-c,pdf,pdf-generation,Ios,Objective C,Pdf,Pdf Generation,我在使用imageFromPDFWithDocumentRef获取PDF封面时遇到了一个奇怪的问题 代码如下 - (UIImage *)imageFromPDFWithDocumentRef:(CGPDFDocumentRef)documentRef { CGPDFPageRef pageRef = CGPDFDocumentGetPage(documentRef, 1); CGRect pageRect = CGPDFPageGetBoxRect(pageRef, kCGPD

我在使用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文件的名称)

这就是我的本意


我想知道是否有人能帮我一把,并提前表示感谢。:)

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

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;
}
使用此代码:

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坐标系如何映射到图像/屏幕坐标系的更详细说明,请参见我博客上的这篇文章(此代码取自此处):

这真是一个及时的帮助!你能告诉我如何在你的代码中获得旋转吗?int rotate=cgpdfpage getrotationangle(pageRef);我简直无法形容你帮了我多少忙。非常感谢,问题已经圆满解决了!