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 ?
CodePudding user response:
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")
}
}