Last active
January 28, 2021 01:47
-
-
Save woodycatliu/38803c53c74fe7320cc2c8ba654c8f3d to your computer and use it in GitHub Desktop.
Swift AVFoundation Orientation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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