【问题标题】:Images of smaller page size PDF are not autoresized to full screen in iOS6较小页面大小的 PDF 图像在 iOS 6 中无法全屏显示
【发布时间】:2023-03-03 16:52:01
【问题描述】:

我正在尝试从 PDF 捕获图像并将它们显示在我的 iPad 应用程序中的 UIScrollView 中。当 pdf 页面大小为 1024 x 768(标准 4:3 纵横比)时,应用程序将图像呈现为全屏(全尺寸)。但是当 pdf 页面的大小为 720 x 540(相同的 4:3 纵横比但尺寸更小)时,我看到图像周围有白色边框。下面是我尝试捕获图像的代码 sn-p。

CGPDFPageRef thePDFPageRef = CGPDFDocumentGetPage(pdfDocRef, pdfIterator);
CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB(); // RGB color space

        CGBitmapInfo bmi = (kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);
        thumbnailWidth = 1024;
        thumbnailHeight = 768;
        CGContextRef context = CGBitmapContextCreate(NULL, thumbnailWidth, thumbnailHeight, 8, 0, rgb, bmi);


        if (context != NULL) // Must have a valid custom CGBitmap context to draw into
        {
            CGRect thumbRect = CGRectMake(0.0f, 0.0f, thumbnailWidth, thumbnailHeight); // Target thumb rect

            CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f);

            CGContextFillRect(context, thumbRect); // White fill

            CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(thePDFPageRef, kCGPDFCropBox, thumbRect, 0, true)); // Fit rect

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

            CGContextDrawPDFPage(context, thePDFPageRef); // Render the PDF page into the custom CGBitmap context

            imageRef = CGBitmapContextCreateImage(context); // Create CGImage from custom CGBitmap context

            CGContextRelease(context); // Release custom CGBitmap context reference
        }

        CGColorSpaceRelease(rgb); // Release device RGB color space reference

        CGPDFPageRelease(thePDFPageRef);
    }


    UIImage *image = [UIImage imageWithCGImage:imageRef scale:scale orientation:0];

即使 pdf 页面较小,如何自动缩放到全屏?

【问题讨论】:

    标签: ios6 uiimageview core-graphics


    【解决方案1】:

    我找到了如下解决方案,缩放为矩形。

    (1) 使用获取PDF页面矩形

    CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
    

    (2) 缩放矩形并将变换应用于身份矩阵

    CGAffineTransform trans = CGAffineTransformIdentity;
    trans = CGAffineTransformTranslate(trans, 0, pageRect.size.height);
    trans = CGAffineTransformScale(trans, 1.0, -1.0);
    rect = CGRectApplyAffineTransform(rect, trans);
    

    (3) 使用 kCGPDFMediaBox 代替 kCGPDFCropBox 以避免在大小与标准不同时裁剪 pdf。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-11
      • 2015-06-28
      • 1970-01-01
      • 2020-12-18
      • 2014-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多