Swift 如何使用 UIImagePickerController 在 cameraOverlayView 上旋转图像

How to rotate image on cameraOverlayView with UIImagePickerController in Swift

我使用以下代码为 UIImagePickerController 创建 cameraOverlayView:

(使用 Xcode 13.2.1 )

if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera)
        {
        print("device have a camera")
        
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self as UIImagePickerControllerDelegate & UINavigationControllerDelegate
        imagePicker.sourceType = UIImagePickerController.SourceType.camera
        
        let imageOverlayName = getimageOverlayNameByPosicaoIdFoto(posicaoIdFoto) // just return a string with an Image name
        
        let mainView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
        let imageOverlay = UIImage(named: imageOverlayName)
        let imageViewOverlay = UIImageView(image: imageOverlay!)
        
        let numberToAllowTopControlsToAppear:CGFloat = 18
        let positionY = self.view.frame.size.height / numberToAllowTopControlsToAppear
        
        let numberToAllowBottomControlsToAppear:CGFloat = 5.5
        let bottomBarHeightSize = self.view.frame.size.height - ( self.view.frame.size.height / numberToAllowBottomControlsToAppear )

        imageViewOverlay.frame = CGRect(x: 0, y: positionY, width: self.view.frame.size.width, height: bottomBarHeightSize)
        
        mainView.isUserInteractionEnabled = false
        mainView.addSubview(imageViewOverlay)
        imagePicker.cameraOverlayView = mainView
        
        self.present(imagePicker, animated: true, completion: nil)
    }

此代码运行良好,但图像固定在屏幕上。 如果用户更改设备方向(通过旋转设备),我如何旋转叠加层中使用的图像?

我只是使用下面的代码来跟踪用户是否旋转了设备

// use this method inside viewDidLoad
NotificationCenter.default.addObserver(self, selector: #selector(deviceOrientationDidChange), name: UIDevice.orientationDidChangeNotification, object: nil) 

@objc func deviceOrientationDidChange() {
    
    switch UIDevice.current.orientation {
    case .portrait:
        print("Portrait")
    case .portraitUpsideDown:
        print("Portrait upside down")            
    case .landscapeLeft:
        print("Landscape left")
    case .landscapeRight:
        print("Landscape right")
    case .faceDown:
        print("Face down")
    case .faceUp:
        print("Face up")
    case .unknown:
        printLog("position unknown")
    @unknown default:
        printLog("unknow - future new methods can be caught here")
    }
}