1

Description

I am having issue with UIImage "generated" from PDFPage via method .thumbnail(of:for). UIImage, which I later display in UIImageView, does not respect rotation of the PDF page, in iOS 13 only.

When PDF page is rotated for example by 90° and presented in PDFView all is good and shown correctly. But when I want to take the UIImage (i.e. thumbnail) of the PDFPage and show it in UIImageView, for some reason, iOS 13 does not respect the initial rotation of the PDF page and present it in UIImageView incorrectly.

Please behold on below sample code and example screenshots.

Testing environment

  • iOS 13 on simulator only, not having real device
  • Xcode 13.1

Sample code

for sake of shorter example, I force unwrap where optionals occur


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),
        ])
    }
}

Example screenshots from simulator

iOS 15, shown correctly

iOS 13.2, shown incorrectly

peetadelic
  • 104
  • 5

0 Answers0