1

I have a PDFView in a container view which has a pinch gesture recognizer. When I zoom on a real device memory gradually peaks up to 2-3 gb and crashes. On simulator memory won't even go above 20mb on a 100 pages sample pdf. What seems to be the problem?

class ViewController: UIViewController {

    var containerView: UIView = UIView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.addSubview(containerView)
        containerView.frame = self.view.frame.insetBy(dx: 100, dy: 100)
        containerView.backgroundColor = .black
        
        loadPDF()
        addGesture()
    }

    var pdfView: CustomPDFView = CustomPDFView()
    func loadPDF() {
        containerView.addSubview(pdfView)
        pdfView.frame = CGRect(x: 50, y: 50, width: 400, height: 500)
        
        let pdfURL = Bundle.main.url(forResource: "A100page47", withExtension: "pdf")
        
        pdfView.autoresizesSubviews = true
        pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleTopMargin, .flexibleLeftMargin]
        pdfView.displayDirection = .vertical

        pdfView.autoScales = true
        pdfView.displayMode = .singlePage
        pdfView.displaysPageBreaks = false
        pdfView.pageShadowsEnabled = false
        pdfView.document = PDFDocument(url: pdfURL!)
        
        /// tryings for memory
        pdfView.interpolationQuality = .none
        pdfView.scalesLargeContentImage = false
        /// tryings for memory
    }

    func addGesture() {
        let gesture = UIPinchGestureRecognizer(target: self, action: #selector(pinchHandler(_:)))
        containerView.addGestureRecognizer(gesture)
    }
    @objc func pinchHandler(_ gesture: UIPinchGestureRecognizer) {
        guard let view = gesture.view else { return }
        let scale = gesture.scale * 2
        view.transform = CGAffineTransform(scaleX: scale, y: scale)
        }
    }
}

CustomPDFView is a simple "no zoom" class

lass CustomPDFView: PDFView {
    override func layoutSubviews() {
        super.layoutSubviews()
        self.minScaleFactor = self.scaleFactor
        self.maxScaleFactor = self.scaleFactor
    }
}
x670
  • 75
  • 6
  • What is `UIPinchGestureRecognizer` for? Since you have the `autoScales` switch set to `true`, there is no point of using it, is there? – El Tomato Oct 27 '21 at 23:41
  • For the case if I have multiple PDF documents in same container view. I want them to scale all together. – x670 Oct 28 '21 at 06:09
  • Did you happen to figure out a solution? I'm having the same problem with a single PDFView with zooming disabled, and a magnification gesture in swiftUI. – Husker28 May 26 '22 at 10:08
  • @Husker28, Unfortunately no. My best shot was drawing PDF Page to CGContext and use it as a UIImage. – x670 May 26 '22 at 13:50

0 Answers0