[英]Fix orientation of images taken with front camera ios
I'm using custom camera implementation in my app using Swift.我正在使用 Swift 在我的应用程序中使用自定义相机实现。 When the image is captured, is called func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?)
.当图像被捕获时,调用func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?)
。 I get the image data with photo.fileDataRepresentation()
and after that I'm fixing the orientation of the photo using the following extension on UIImage
.我使用photo.fileDataRepresentation()
获取图像数据,然后使用UIImage
上的以下扩展名修复照片的方向。
func fixedOrientation() -> UIImage? {
guard imageOrientation != UIImage.Orientation.up else {
// This is default orientation, don't need to do anything
return self.copy() as? UIImage
}
guard let cgImage = self.cgImage else {
// CGImage is not available
return nil
}
guard let colorSpace = cgImage.colorSpace, let ctx = CGContext(data: nil, width: Int(size.width), height: Int(size.height), bitsPerComponent: cgImage.bitsPerComponent, bytesPerRow: 0, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else {
return nil // Not able to create CGContext
}
var transform = CGAffineTransform.identity
switch imageOrientation {
case .down, .downMirrored:
transform = transform.translatedBy(x: size.width, y: size.height)
transform = transform.rotated(by: CGFloat.pi)
case .left, .leftMirrored:
transform = transform.translatedBy(x: size.width, y: 0)
transform = transform.rotated(by: CGFloat.pi / 2.0)
case .right, .rightMirrored:
transform = transform.translatedBy(x: 0, y: size.height)
transform = transform.rotated(by: CGFloat.pi / -2.0)
case .up, .upMirrored:
break
@unknown default:
break
}
// Flip image one more time if needed to, this is to prevent flipped image
switch imageOrientation {
case .upMirrored, .downMirrored:
transform = transform.translatedBy(x: size.width, y: 0)
transform = transform.scaledBy(x: -1, y: 1)
case .leftMirrored, .rightMirrored:
transform = transform.translatedBy(x: size.height, y: 0)
transform = transform.scaledBy(x: -1, y: 1)
case .up, .down, .left, .right:
break
@unknown default:
break
}
ctx.concatenate(transform)
switch imageOrientation {
case .left, .leftMirrored, .right, .rightMirrored:
ctx.draw(cgImage, in: CGRect(x: 0, y: 0, width: size.height, height: size.width))
default:
ctx.draw(cgImage, in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
}
guard let newCGImage = ctx.makeImage() else { return nil }
return UIImage(cgImage: newCGImage, scale: 1, orientation: .up)
}
Apparently this is working fine for images taken with back camera, but taking with frontal one I'm facing issues.显然,这对于使用后置摄像头拍摄的图像效果很好,但使用正面拍摄时我遇到了问题。
Note : I'm also changing the videoOrientation
from AVCaptureConnection
when rotating the device.注意:旋转设备时,我还从AVCaptureConnection
更改了videoOrientation
。
It turned out that i was changing the videoOrientation
when rotation did change, which was wrong.事实证明,当旋转确实发生变化时,我正在更改videoOrientation
,这是错误的。 I had to move that logic before capturing the photo.在拍摄照片之前,我必须改变这个逻辑。 Now it works fine.现在它工作正常。 Also I've fixed the mirroring problem, by setting isVideoMirrored
to true if using the front camera.此外,如果使用前置摄像头,我通过将isVideoMirrored
设置为 true 来解决镜像问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.