2017-10-23 5 views
0

私はポートレートモードで写真を撮るカスタムカメラを持っています。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) 
    } 
} 

} 画像はプレビューで正しい向きで表示されますが、保存するとポートレートになります。

答えて

1

あなたがvideoDeviceOrientationを得るためにあなたのmanagePhotoOrientation()関数を使用することができ

if let photoOutputConnection = CustomCamera.photoOutput.connection(with: AVMediaType.video) { 
    photoOutputConnection.videoOrientation = videoDeviceOrientation 
} 

)capturePhotoを(呼び出す前に次のコードを実行する必要があります。

+0

答えてくれてありがとう...アップルのアドバイスを参考にしてコードを完全に変更しました... 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

+0

これは完全に動作します... – Marco

+0

AVCamは私の写真撮影に基づいています。あなたはそのコードでvideoOrientationの設定を参照する必要があります – Spads

0

私は同様の問題を抱えていましたが、 aken写真:

UIGraphicsBeginImageContextWithOptions(image.size, false, image.scale) 
image.draw(in: CGRect(origin: .zero, size: image.size)) 
let newImage = UIGraphicsGetImageFromCurrentImageContext() 
UIGraphicsEndImageContext() 

どうやらiOS版によって生成(オリエンテーションを含みます)画像のメタデータは不完全であり、一部の視聴者が(私の場合ChromeとSafariのブラウザで)正しく画像をレンダリングするために失敗します。再レンダリングにより、メタデータはすべての視聴者に正しく設定されているように見えます。

私のアプリでは、私も画像をダウンスケーリングしましたが、これは効果は変わっていないと思っています.2)他の方法で画像をキャプチャしています(つまりUIImagePickerController)。まだ、あなたは私のハッキーの修正を試してみてください。

+0

お返事ありがとうございます。問題は、たとえ私が新しい画像を描いても、それはまだポートレートモードに残っています...問題は、私が電話を回転させるときに新しいプレビエイヤーを追加しないということです... – Marco

+0

明瞭にするために、画像は_ app_と_はapp_で表示されていますが、依然として向きが間違っていますか? –

+0

いいえ....アプリでは正しい方向性を持っていますが、私はそれを保存すると肖像画に行きます...私のコードを少し変更しました...私の回答を更新します – Marco

関連する問題