gpt4 book ai didi

ios - 仅支持竖屏界面时如何保存相机图像

转载 作者:行者123 更新时间:2023-11-29 05:53:34 25 4
gpt4 key购买 nike

我有一个使用 AVCapturePhotoOutput 拍照的 View Controller 。我已经锁定了 View Controller 的可能方向,以便相机预览不会旋转:

override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
return .portrait
}

问题 1:自从我改变了这一点后,当我拍照时,无论设备处于纵向还是横向模式,结果图像始终具有 UIImageOrientation == .right

问题2:然后我想使用UIImageJPEGRepresentation将图像保存到文件系统,但是这个方法不包含相对于方向的exif信息(目前还可以,因为方向由于问题 1),目前是错误的。

我只想做许多其他应用程序正在做的事情:显示相机预览,当设备旋转时,该预览不会旋转,但拍摄的图像具有正确的方向,并且我可以保存它们。

有没有办法做到这一点而不必使用绘图方法旋转图像的数据?

最佳答案

问题是当锁定界面方向时:

override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
return .portrait
}

预览层视频方向没有更新,这一点现在很明显。由于界面方向已锁定,因此我使用设备方向来告诉连接要使用哪个方向。这样缓冲区数据将具有正确的方向

@IBAction func shutterButtonDidClick(_ sender: Any) {

guard let output = self.cameraSession.outputs.compactMap({ $0 as? AVCapturePhotoOutput }).first, let photoOutputConnection = output.connection(with: .video), let delegate = self.videoCaptureDelegate else { return }
let deviceOrientation = UIDevice.current.orientation
//Important line
photoOutputConnection.videoOrientation = AVCaptureVideoOrientation(deviceOrientation: deviceOrientation)
let photoSettings = AVCapturePhotoSettings()
photoSettings.isHighResolutionPhotoEnabled = true
photoSettings.flashMode = output.supportedFlashModes.contains(.auto) ? .auto : .off
photoSettings.isAutoStillImageStabilizationEnabled =
output.isStillImageStabilizationSupported
output.capturePhoto(with: photoSettings, delegate: delegate)
}

extension AVCaptureVideoOrientation {
init(deviceOrientation: UIDeviceOrientation) {
switch deviceOrientation {
case .portrait: self = .portrait
case .portraitUpsideDown: self = .portraitUpsideDown
case .landscapeLeft: self = .landscapeRight
case .landscapeRight: self = .landscapeLeft
default: self = .portrait
}
}
}

关于ios - 仅支持竖屏界面时如何保存相机图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55424200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com