私はポートレートモードで写真を撮るカスタムカメラを持っています。Swift - AVFoundationキャプチャ画像の向き
私の問題は、風景で写真を撮って保存しようとすると、写真がポートレートモードで保存されるということです。
は私がpreviewLayerセットアップにこの機能を持っている:
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)
}
}
}
すべてがいる限り、私はポートレートモードの写真を撮影したが、ときに私が回転として正常に動作し、写真を撮るよりも
私は風景写真を撮影するための電話は、写真はまだ肖像画で撮影されています。
私はカスタムカメラで作業するのは初めてですが、これを修正するためにどこから始めるのかは本当に分かりません。 誰かが本当に感謝してくれるのであれば助けてください。
ありがとうございました!
--------------- UPDATE 私はこの機能を追加しました:
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)
}
}
} 画像はプレビューで正しい向きで表示されますが、保存するとポートレートになります。
答えてくれてありがとう...アップルのアドバイスを参考にしてコードを完全に変更しました... https://developer.apple.com/library/content/samplecode/AVCam/Listings/Swift_AVCam_CameraViewController_swift.html#//apple_ref/doc/uid/DTS40010112-Swift_AVCam_CameraViewController_swift-DontLinkElementID_15 – Marco
これは完全に動作します... – Marco
AVCamは私の写真撮影に基づいています。あなたはそのコードでvideoOrientationの設定を参照する必要があります – Spads