         CVPixelBufferUnlockBaseAddress(imageBuffer, CVPixelBufferLockFlags(rawValue: 0))
         
         guard let rotateCtx = CGContext(data: nil, width: height, height: width, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo) else {return nil }
-        rotateCtx.translateBy(x: 0.0, y: CGFloat(width))
-        rotateCtx.rotate(by: -CGFloat.pi * 0.5)
+        if let transform = applying?(CGSize(width: width, height: height)) {
+            rotateCtx.concatenate(transform)
+        } else {
+            rotateCtx.translateBy(x: 0.0, y: CGFloat(width))
+            rotateCtx.rotate(by: -CGFloat.pi * 0.5)
+        }
         rotateCtx.draw(_originalImage, in: CGRect(origin: .zero, size: CGSize(width: width, height: height)))
         guard let _image = rotateCtx.makeImage() else { return nil }
 

}

extension UIImage {
    public class func image(from sampleBuffer: CMSampleBuffer) -> UIImage? {
        // Get a CMSampleBuffer's Core Video image buffer for the media data
        guard let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return nil }
        // Lock the base address of the pixel buffer

        
        UIDevice.current.beginGeneratingDeviceOrientationNotifications()
        NotificationCenter.default.addObserver(self, selector: #selector(_handleOrientationDidChange(_:)), name: .UIDeviceOrientationDidChange, object: nil)
        
        imagePickerController._captureDisplayQueue.async { [weak self] in
            guard let wself = self else { return }
            if !wself.imagePickerController._captureDisplayViews.contains(wself._captureDisplayView) {
