Crash on UIGraphicsGetImageFromCurrentImageContext from UIScrollView

The following code is supposed to create images from a UIScrollView but crashes due to memory issues which I cannot seem to be able to fix.

func create(fromScrollView scrollView: UIScrollView) {

    let pageHeight = 2480
    let pageWidth = 1748

    let currentPageHeight = scrollView.frame.height

    let pdfData = NSMutableData()

    var imageURLS = [URL]()

    let pageCount = Int(ceil(scrollView.contentSize.height / CGFloat(currentPageHeight)))

    for page in 0..<pageCount {
            UIGraphicsBeginImageContextWithOptions(CGSize(width: pageWidth,
                                                          height: pageHeight),
            if let context = UIGraphicsGetCurrentContext() {
                context.scaleBy(x: CGFloat(pageHeight)/scrollView.frame.height,
                                y: CGFloat(pageHeight)/scrollView.frame.height)

                let scrollOffset = Int(currentPageHeight) * page
                scrollView.contentOffset = CGPoint(x: 0,
                                                   y: scrollOffset)
                context.translateBy(x: 0,
                                    y: CGFloat(-scrollOffset))

                scrollView.layer.render(in: context)

                var image = UIGraphicsGetImageFromCurrentImageContext()

                if let image = image,
                    let url = saveImageToTempDirectory(image: image, withName: "\(page)") {
                image = nil
// some work which is done with the urls and the pdfData later on but the program already crashes before this point

and this helper function

private func saveImageToTempDirectory(image: UIImage, withName name: String) -> URL? {
    var url: URL?
        if let data = UIImagePNGRepresentation(image) {
            let imageFileUrl = URL(fileURLWithPath: NSTemporaryDirectory().appending("\(name)"))
            do {
                try data.write(to: imageFileUrl)
                print("Successfully saved image at path: \(imageFileUrl)")
                url = imageFileUrl
            } catch {
                print("Error saving image: \(error)")
    return url

As you can see I already tried using an autoreleasepool to no avail. Memory consumption of the app resizes steadily for long UIScrollViews and the app crashes eventually. I also tried this

scrollView.layer.contents = nil

Setting the image to nil does also not work

image = nil

Rendering the images from the UIScrollView in a different way did not work as this seemed to be the only way to retain the original look of the view. I used some other methods to get the image from the UIScrollView but somehow text ended up getting rendered in the wrong color for example.

Also tried the suggestions from the following links which did not work

swift UIGraphicsGetImageFromCurrentImageContext can't release memory

UIGraphicsGetImageFromCurrentImageContext memory leak with previews

What can I do to solve my memory issues? Is it an issue with the CALayer?