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

发布于 2025-01-10 00:38:20 字数 1682 浏览 0 评论 0原文

我有以下代码,用于为 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)
    }

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

I have the following code that I use to create an cameraOverlayView for a UIImagePickerController:

( using 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)
    }

This code works perfectly, but the image is fixed in the screen.
If the user change the device orientation(by rotating the device), how can I rotate the image used in the overlay ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一向肩并 2025-01-17 00:38:20

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

// 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")
    }
}

I just use the following code to track if the user rotate the device

// 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")
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文