         guard !imageSize.equalTo(.zero) else { return nil }
         
         UIGraphicsBeginImageContextWithOptions(imageSize, false, UIScreen.main.scale)
+        defer { UIGraphicsEndImageContext() }
         guard let context = UIGraphicsGetCurrentContext() else { return nil }
         ligature.draw(at: .zero)
         guard let cgImage = UIGraphicsGetImageFromCurrentImageContext()?.cgImage else { return nil }

            }
            // Draw the current page image.
            UIGraphicsBeginImageContextWithOptions(imageSize, false, UIScreen.main.scale)
            guard let context = UIGraphicsGetCurrentContext() else { return }
            context.scaleBy(x: 1.0, y: -1.0)
            context.translateBy(x: 0.0, y: -imageSize.height)
            let scale = min(imageSize.width / mediaRect.width, imageSize.height / mediaRect.height)

import UIKit
import ImageIO
import Accelerate
import Foundation
import CoreGraphics
import AVFoundation

