我有一个自定义相机,可以以纵向模式拍照。
我的问题是,如果我尝试在横向模式下拍照并保存它,那么照片仍然以纵向模式保存。
我有这个函数来设置预览层:
我在viewWillAppear中调用这个方法(我认为这不是正确的做法)。
然后我拍照:
并使用此扩展:
只要我拍摄肖像模式照片,一切都正常,但当我将手机旋转以拍摄横向照片时,照片仍然是以纵向拍摄的。
这是我第一次使用自定义相机,我真的不知道从哪里开始修复它... 如果有人能帮忙,我会非常感激。
谢谢!
---------------更新 我已经添加了这个功能:
并将我的扩展名更改为以下内容:
这个图像在预览时显示的方向是正确的,但当我保存它时,它变成了纵向。
我的问题是,如果我尝试在横向模式下拍照并保存它,那么照片仍然以纵向模式保存。
我有这个函数来设置预览层:
static func setupPreviewLayer(view: UIView) {
cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
switch UIDevice.current.orientation {
case .portrait:
cameraPreviewLayer?.connection?.videoOrientation =
AVCaptureVideoOrientation.portrait
case .portraitUpsideDown:
cameraPreviewLayer?.connection?.videoOrientation =
AVCaptureVideoOrientation.portraitUpsideDown
case .landscapeLeft:
cameraPreviewLayer?.connection?.videoOrientation =
AVCaptureVideoOrientation.landscapeLeft
case .landscapeRight:
cameraPreviewLayer?.connection?.videoOrientation =
AVCaptureVideoOrientation.landscapeRight
default:
cameraPreviewLayer?.connection?.videoOrientation =
AVCaptureVideoOrientation.portrait
}
cameraPreviewLayer?.frame = view.frame
view.layer.insertSublayer(cameraPreviewLayer!, at: 0)
}
我在viewWillAppear中调用这个方法(我认为这不是正确的做法)。
然后我拍照:
@IBAction func takePhotoBtnPressed(_ sender: Any) {
let settings = AVCapturePhotoSettings()
useFlash(settings: settings)
settings.isAutoStillImageStabilizationEnabled = true
CustomCamera.photoOutput?.capturePhoto(with: settings, delegate: self)
}
并使用此扩展:
extension FishCameraVC: AVCapturePhotoCaptureDelegate {
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
if let imageData = photo.fileDataRepresentation() {
image = UIImage(data: imageData)
performSegue(withIdentifier: "ShowTakenPhoto", sender: nil)
}
}
}
只要我拍摄肖像模式照片,一切都正常,但当我将手机旋转以拍摄横向照片时,照片仍然是以纵向拍摄的。
这是我第一次使用自定义相机,我真的不知道从哪里开始修复它... 如果有人能帮忙,我会非常感激。
谢谢!
---------------更新 我已经添加了这个功能:
func managePhotoOrientation() -> UIImageOrientation {
var currentDevice: UIDevice
currentDevice = .current
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
var deviceOrientation: UIDeviceOrientation
deviceOrientation = currentDevice.orientation
var imageOrientation: UIImageOrientation!
if deviceOrientation == .portrait {
imageOrientation = .up
print("Device: Portrait")
}else if (deviceOrientation == .landscapeLeft){
imageOrientation = .left
print("Device: LandscapeLeft")
}else if (deviceOrientation == .landscapeRight){
imageOrientation = .right
CustomCamera.cameraPreviewLayer?.connection?.videoOrientation = .landscapeRight
print("Device LandscapeRight")
}else if (deviceOrientation == .portraitUpsideDown){
imageOrientation = .down
print("Device PortraitUpsideDown")
}else{
imageOrientation = .up
}
return imageOrientation
}
并将我的扩展名更改为以下内容:
extension FishCameraVC: AVCapturePhotoCaptureDelegate {
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
if let imageData = photo.fileDataRepresentation() {
image = UIImage(data: imageData)
let dataProvider = CGDataProvider(data: imageData as CFData)
let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: .defaultIntent)
self.image = UIImage(cgImage: cgImageRef!, scale: 1.0, orientation: imageOrientation!)
performSegue(withIdentifier: "ShowTakenPhoto", sender: nil)
}
}
}
这个图像在预览时显示的方向是正确的,但当我保存它时,它变成了纵向。