简体   繁体   中英

PDFView causes app to freeze on iOS 16, only when autoscales = true

I am using a PDFView to display images in my app (built using SwifUI), simply for quick and easy pinch-to-zoom functionality. This worked perfectly in iOS 15, but since updating to iOS 16, the app freezes when attempting to load the image viewer ( PhotoDetailView below). The issue persists across both the simulator and a physical device.

Here is the code I'm using:

import SwiftUI
import PDFKit

struct PhotoDetailView: UIViewRepresentable {
    let image: UIImage

    func makeUIView(context: Context) -> PDFView {
        let view = PDFView()
        view.document = PDFDocument()
        guard let page = PDFPage(image: image) else { return view }
        view.document?.insert(page, at: 0)
        view.autoScales = true
        return view
    }

    func updateUIView(_ uiView: PDFView, context: Context) {
        // empty
    }
}

When I run the code on iOS 16.0 in the simulator, I get 2 errors in the console:

  1. [Assert] -[UIScrollView _clampedZoomScale:allowRubberbanding:]: Must be called with non-zero scale
  2. [Unknown process name] CGAffineTransformInvert: singular matrix.

I have been able to isolate the issue to view.autoscales = true . If I print view.scaleFactor , I can see that it is 1.0 before the autoscale and 0.0 afterward (which is what appears to be prompting the errors). These errors also show up in the console when using iOS 15 in the simulator, but the images load as expected.

When view.autoscales = true is commented out, the image loads, albeit at a size that is much larger than the device screen.

Does anyone have any idea what may be causing this? I'd really like to avoid having to build a custom viewer, since I'm just trying to let users quickly pinch to zoom on images.

I managed to resolve this issue. I'm posting my solution here, in case anyone else runs into the same type of problem.

The issue only occurred when using a NavigationLink to navigate to PhotoDetailView . This led me to look more closely at my navigation stack.

After some digging, I found that the problem was related to my use of .navigationViewStyle(.stack) on the NavigationView . I needed this modifier to get things to display correctly on iOS 15, but the same modifier was breaking my image viewer with iOS 16.

My solution was to create a custom container to replace NavigationView , which conditionally uses NavigationStack for iOS 16, or NavigationView + .navigationViewStyle(.stack) for iOS 15. That worked like a charm and my image viewer is back in action.

I found inspiration for my custom container here: https://developer.apple.com/forums/thread/710377

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM