1

描述

我对via method的UIImage“生成”有疑问。,我稍后在 iOS 13 中显示,不考虑 PDF 页面的旋转。PDFPage.thumbnail(of:for)UIImageUIImageView

例如,当 PDF 页面旋转 90° 并呈现出来时,PDFView一切都很好并且正确显示。但是当我想获取UIImage(即缩略图)PDFPage并显示它时UIImageView,出于某种原因,iOS 13 不尊重 PDF 页面的初始旋转并UIImageView错误地显示它。

请看下面的示例代码和示例截图。

测试环境

  • iOS 13 仅在模拟器上,没有真实设备
  • Xcode 13.1

示例代码

为了更简短的例子,我强制展开选项出现的地方


import UIKit
import PDFKit

class TestingViewController: UIViewController {

    lazy var imageView: UIImageView = {
        let iv = UIImageView()
        iv.translatesAutoresizingMaskIntoConstraints = false
        return iv
    }()
    
    // Pass URL of the PDF document when instantiating this VC
    init(withURL: URL) {
        super.init(nibName: nil, bundle: nil)
        let pdfDoc = PDFDocument(url: withURL)
        let pdfPage = pdfDoc!.page(at: 0) // Getting first page of the doc
        let pdfPageRect = pdfPage!.bounds(for: .mediaBox)
        imageView.image = pdfPage!.thumbnail(of: pdfPageRect.size, for: .mediaBox) // Getting the UIImage of the PDF page and assign it to the needed image view
        imageView.contentMode = .scaleAspectFit
        imageView.clipsToBounds = true
        print("\(pdfPage?.rotation)") // Prints the correct rotation value, even though UIImageView does not respect it
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // Layout only
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemPink
        view.addSubview(imageView)
        
        NSLayoutConstraint.activate([
            imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            imageView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            imageView.widthAnchor.constraint(equalToConstant: 350),
            imageView.heightAnchor.constraint(equalToConstant: 700),
        ])
    }
}

模拟器截图示例

iOS 15,正确显示

iOS 13.2,显示不正确

4

0 回答 0