Skip to content
This repository was archived by the owner on Jul 1, 2022. It is now read-only.

Commit b2e1d72

Browse files
Flip front camera image so they match the preview that was shown
1 parent 30eeb63 commit b2e1d72

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

ALCameraViewController/Utilities/CameraShot.swift

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import AVFoundation
1111

1212
public 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
})

ALCameraViewController/Views/CameraView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public class CameraView: UIView {
162162
let size = frame.size
163163

164164
cameraQueue.sync {
165-
takePhoto(output, videoOrientation: orientation, cropSize: size) { image in
165+
takePhoto(output, videoOrientation: orientation, cameraPosition: device.position, cropSize: size) { image in
166166
DispatchQueue.main.async() { [weak self] in
167167
self?.isUserInteractionEnabled = true
168168
completion(image)

0 commit comments

Comments
 (0)