Skip to content

Instantly share code, notes, and snippets.

@woodycatliu
Last active January 28, 2021 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woodycatliu/38803c53c74fe7320cc2c8ba654c8f3d to your computer and use it in GitHub Desktop.
Save woodycatliu/38803c53c74fe7320cc2c8ba654c8f3d to your computer and use it in GitHub Desktop.
Swift AVFoundation Orientation
func deviceExifOrientationForBackCamera(_ deviceOrientation: UIDeviceOrientation = UIDevice.current.orientation) -> CGImagePropertyOrientation{
switch deviceOrientation {
case .portrait:
return .right
case .landscapeLeft:
return .up
case .landscapeRight:
return .down
default:
return .right
}
}
func deviceExifOrientationForFrontCamera(_ deviceOrientation: UIDeviceOrientation = UIDevice.current.orientation) -> CGImagePropertyOrientation {
switch deviceOrientation {
case .portraitUpsideDown:
return .rightMirrored
case .landscapeLeft:
return .downMirrored
case .landscapeRight:
return .upMirrored
default:
return .leftMirrored
}
}
extension CGImagePropertyOrientation {
init(isUsingFrontFacingCamera: Bool, deviceOrientation: UIDeviceOrientation = UIDevice.current.orientation) {
switch deviceOrientation {
case .portrait:
self = .right
case .portraitUpsideDown:
self = .left
case .landscapeLeft:
self = isUsingFrontFacingCamera ? .down : .up
case .landscapeRight:
self = isUsingFrontFacingCamera ? .up : .down
default:
self = .right
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment