@@ -11,7 +11,7 @@ import AVFoundation
1111
1212public typealias CameraShotCompletion = ( UIImage ? ) -> Void
1313
14- public func takePhoto( _ stillImageOutput: AVCaptureStillImageOutput , videoOrientation: AVCaptureVideoOrientation , cropSize: CGSize , completion: @escaping CameraShotCompletion ) {
14+ public func takePhoto( _ stillImageOutput: AVCaptureStillImageOutput , videoOrientation: AVCaptureVideoOrientation , cameraPosition : AVCaptureDevicePosition , cropSize: CGSize , completion: @escaping CameraShotCompletion ) {
1515
1616 guard let videoConnection: AVCaptureConnection = stillImageOutput. connection ( withMediaType: AVMediaTypeVideo) else {
1717 completion ( nil )
@@ -24,10 +24,32 @@ public func takePhoto(_ stillImageOutput: AVCaptureStillImageOutput, videoOrient
2424
2525 guard let buffer = buffer,
2626 let imageData = AVCaptureStillImageOutput . jpegStillImageNSDataRepresentation ( buffer) ,
27- let image = UIImage ( data: imageData) else {
27+ var image = UIImage ( data: imageData) else {
2828 completion ( nil )
2929 return
3030 }
31+
32+ // flip the image to match the orientation of the preview
33+ if cameraPosition == . front, let cgImage = image. cgImage {
34+ switch image. imageOrientation {
35+ case . leftMirrored:
36+ image = UIImage ( cgImage: cgImage, scale: image. scale, orientation: . right)
37+ case . left:
38+ image = UIImage ( cgImage: cgImage, scale: image. scale, orientation: . rightMirrored)
39+ case . rightMirrored:
40+ image = UIImage ( cgImage: cgImage, scale: image. scale, orientation: . left)
41+ case . right:
42+ image = UIImage ( cgImage: cgImage, scale: image. scale, orientation: . leftMirrored)
43+ case . up:
44+ image = UIImage ( cgImage: cgImage, scale: image. scale, orientation: . upMirrored)
45+ case . upMirrored:
46+ image = UIImage ( cgImage: cgImage, scale: image. scale, orientation: . up)
47+ case . down:
48+ image = UIImage ( cgImage: cgImage, scale: image. scale, orientation: . downMirrored)
49+ case . downMirrored:
50+ image = UIImage ( cgImage: cgImage, scale: image. scale, orientation: . down)
51+ }
52+ }
3153
3254 completion ( image)
3355 } )
0 commit comments