mirror of
https://github.com/TelegramMessenger/Telegram-iOS.git
synced 2026-07-06 03:33:41 +02:00
Fix frontal camera photo orientation on iPad
This commit is contained in:
parent
6e48514c3b
commit
02181ead9e
2 changed files with 30 additions and 40 deletions
|
|
@ -188,16 +188,26 @@ extension CameraPreviewView.Rotation {
|
|||
}
|
||||
}
|
||||
|
||||
func exifOrientationForDeviceOrientation(_ deviceOrientation: UIDeviceOrientation) -> CGImagePropertyOrientation {
|
||||
switch deviceOrientation {
|
||||
case .portraitUpsideDown:
|
||||
return .rightMirrored
|
||||
case .landscapeLeft:
|
||||
return .downMirrored
|
||||
case .landscapeRight:
|
||||
return .upMirrored
|
||||
default:
|
||||
return .leftMirrored
|
||||
func exifOrientation(for orientation: AVCaptureVideoOrientation, mirror: Bool) -> Int32 {
|
||||
switch (orientation, mirror) {
|
||||
case (.portrait, false):
|
||||
return 6
|
||||
case (.portrait, true):
|
||||
return 5
|
||||
case (.portraitUpsideDown, false):
|
||||
return 8
|
||||
case (.portraitUpsideDown, true):
|
||||
return 7
|
||||
case (.landscapeLeft, false):
|
||||
return 3
|
||||
case (.landscapeLeft, true):
|
||||
return 2
|
||||
case (.landscapeRight, false):
|
||||
return 1
|
||||
case (.landscapeRight, true):
|
||||
return 4
|
||||
@unknown default:
|
||||
return 6
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -58,36 +58,16 @@ final class PhotoCaptureContext: NSObject, AVCapturePhotoCaptureDelegate {
|
|||
print("Error occurred while capturing photo: Missing pixel buffer (\(String(describing: error)))")
|
||||
return
|
||||
}
|
||||
|
||||
var photoFormatDescription: CMFormatDescription?
|
||||
CMVideoFormatDescriptionCreateForImageBuffer(allocator: kCFAllocatorDefault, imageBuffer: photoPixelBuffer, formatDescriptionOut: &photoFormatDescription)
|
||||
|
||||
var orientation: UIImage.Orientation = .right
|
||||
if self.orientation == .landscapeLeft {
|
||||
orientation = .down
|
||||
} else if self.orientation == .landscapeRight {
|
||||
orientation = .up
|
||||
} else if self.orientation == .portraitUpsideDown {
|
||||
orientation = .left
|
||||
}
|
||||
|
||||
let finalPixelBuffer = photoPixelBuffer
|
||||
let renderedCIImage = CIImage(cvImageBuffer: finalPixelBuffer)
|
||||
if let cgImage = self.ciContext.createCGImage(renderedCIImage, from: renderedCIImage.extent) {
|
||||
var image = UIImage(cgImage: cgImage, scale: 1.0, orientation: orientation)
|
||||
if image.imageOrientation != .up {
|
||||
UIGraphicsBeginImageContextWithOptions(image.size, true, image.scale)
|
||||
if self.mirror, let context = UIGraphicsGetCurrentContext() {
|
||||
context.translateBy(x: image.size.width / 2.0, y: image.size.height / 2.0)
|
||||
context.scaleBy(x: -1.0, y: 1.0)
|
||||
context.translateBy(x: -image.size.width / 2.0, y: -image.size.height / 2.0)
|
||||
}
|
||||
image.draw(in: CGRect(origin: .zero, size: image.size))
|
||||
if let currentImage = UIGraphicsGetImageFromCurrentImageContext() {
|
||||
image = currentImage
|
||||
}
|
||||
UIGraphicsEndImageContext()
|
||||
}
|
||||
|
||||
//if let value = photo.metadata[kCGImagePropertyOrientation as String] as? NSNumber {
|
||||
// orientation = value.int32Value
|
||||
//} else {
|
||||
let orientation = exifOrientation(for: self.orientation, mirror: self.mirror)
|
||||
//}
|
||||
|
||||
let ci = CIImage(cvImageBuffer: photoPixelBuffer).oriented(forExifOrientation: orientation)
|
||||
if let cgImage = self.ciContext.createCGImage(ci, from: ci.extent) {
|
||||
let image = UIImage(cgImage: cgImage, scale: 1.0, orientation: .up)
|
||||
self.pipe.putNext(.finished(image, nil, CACurrentMediaTime()))
|
||||
} else {
|
||||
self.pipe.putNext(.failed)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue