Skip to content

Commit 02181ea

Browse files
committed
Fix frontal camera photo orientation on iPad
1 parent 6e48514 commit 02181ea

File tree

2 files changed

+30
-40
lines changed

2 files changed

+30
-40
lines changed

submodules/Camera/Sources/CameraUtils.swift

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -188,16 +188,26 @@ extension CameraPreviewView.Rotation {
188188
}
189189
}
190190

191-
func exifOrientationForDeviceOrientation(_ deviceOrientation: UIDeviceOrientation) -> CGImagePropertyOrientation {
192-
switch deviceOrientation {
193-
case .portraitUpsideDown:
194-
return .rightMirrored
195-
case .landscapeLeft:
196-
return .downMirrored
197-
case .landscapeRight:
198-
return .upMirrored
199-
default:
200-
return .leftMirrored
191+
func exifOrientation(for orientation: AVCaptureVideoOrientation, mirror: Bool) -> Int32 {
192+
switch (orientation, mirror) {
193+
case (.portrait, false):
194+
return 6
195+
case (.portrait, true):
196+
return 5
197+
case (.portraitUpsideDown, false):
198+
return 8
199+
case (.portraitUpsideDown, true):
200+
return 7
201+
case (.landscapeLeft, false):
202+
return 3
203+
case (.landscapeLeft, true):
204+
return 2
205+
case (.landscapeRight, false):
206+
return 1
207+
case (.landscapeRight, true):
208+
return 4
209+
@unknown default:
210+
return 6
201211
}
202212
}
203213

submodules/Camera/Sources/PhotoCaptureContext.swift

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -58,36 +58,16 @@ final class PhotoCaptureContext: NSObject, AVCapturePhotoCaptureDelegate {
5858
print("Error occurred while capturing photo: Missing pixel buffer (\(String(describing: error)))")
5959
return
6060
}
61-
62-
var photoFormatDescription: CMFormatDescription?
63-
CMVideoFormatDescriptionCreateForImageBuffer(allocator: kCFAllocatorDefault, imageBuffer: photoPixelBuffer, formatDescriptionOut: &photoFormatDescription)
64-
65-
var orientation: UIImage.Orientation = .right
66-
if self.orientation == .landscapeLeft {
67-
orientation = .down
68-
} else if self.orientation == .landscapeRight {
69-
orientation = .up
70-
} else if self.orientation == .portraitUpsideDown {
71-
orientation = .left
72-
}
73-
74-
let finalPixelBuffer = photoPixelBuffer
75-
let renderedCIImage = CIImage(cvImageBuffer: finalPixelBuffer)
76-
if let cgImage = self.ciContext.createCGImage(renderedCIImage, from: renderedCIImage.extent) {
77-
var image = UIImage(cgImage: cgImage, scale: 1.0, orientation: orientation)
78-
if image.imageOrientation != .up {
79-
UIGraphicsBeginImageContextWithOptions(image.size, true, image.scale)
80-
if self.mirror, let context = UIGraphicsGetCurrentContext() {
81-
context.translateBy(x: image.size.width / 2.0, y: image.size.height / 2.0)
82-
context.scaleBy(x: -1.0, y: 1.0)
83-
context.translateBy(x: -image.size.width / 2.0, y: -image.size.height / 2.0)
84-
}
85-
image.draw(in: CGRect(origin: .zero, size: image.size))
86-
if let currentImage = UIGraphicsGetImageFromCurrentImageContext() {
87-
image = currentImage
88-
}
89-
UIGraphicsEndImageContext()
90-
}
61+
62+
//if let value = photo.metadata[kCGImagePropertyOrientation as String] as? NSNumber {
63+
// orientation = value.int32Value
64+
//} else {
65+
let orientation = exifOrientation(for: self.orientation, mirror: self.mirror)
66+
//}
67+
68+
let ci = CIImage(cvImageBuffer: photoPixelBuffer).oriented(forExifOrientation: orientation)
69+
if let cgImage = self.ciContext.createCGImage(ci, from: ci.extent) {
70+
let image = UIImage(cgImage: cgImage, scale: 1.0, orientation: .up)
9171
self.pipe.putNext(.finished(image, nil, CACurrentMediaTime()))
9272
} else {
9373
self.pipe.putNext(.failed)

0 commit comments

Comments
 (0)