jus10
jus10

Reputation: 51

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.

Upvotes: 1

Views: 693

Answers (2)

Malte
Malte

Reputation: 56

Had the same problem with the PDFView() in iOS 18 still, it froze on some users..

@jus11 your answer has led me to look at my Navigation logic. I use a NavigationStack and I add a few modifier to the view. After some testing it was the modifier .navigationTitle("") which froze everything, very strange. Removed it and worked like a charm.

So thanks for the direction and for the others: Look at your navigation stack.

(Had to post, can't comment yet)

Upvotes: 0

jus10
jus10

Reputation: 51

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

Upvotes: 1

Related Questions