SwiftyCam icon indicating copy to clipboard operation
SwiftyCam copied to clipboard

UI API called on a background thread

Open ohadnav opened this issue 6 years ago • 3 comments

getVideoOrientation() is invoked from a background thread

ohadnav avatar Sep 26 '17 12:09 ohadnav

This code solves this:

  fileprivate func getVideoOrientation() -> AVCaptureVideoOrientation {
    var videoOrientation: AVCaptureVideoOrientation!
    if shouldUseDeviceOrientation && deviceOrientation != nil {
      switch deviceOrientation! {
      case .landscapeLeft:
        videoOrientation = .landscapeRight
      case .landscapeRight:
        videoOrientation = .landscapeLeft
      case .portraitUpsideDown:
        videoOrientation = .portraitUpsideDown
      default:
        videoOrientation = .portrait
      }
    } else {
      DispatchQueue.main.sync {
        videoOrientation = self.previewLayer!.videoPreviewLayer.connection.videoOrientation
      }
    }
    return videoOrientation
  }

ohadnav avatar Sep 26 '17 14:09 ohadnav

Same premise here, but keeping the "guard"

fileprivate func getVideoOrientation() -> AVCaptureVideoOrientation {
    guard shouldUseDeviceOrientation, let deviceOrientation = self.deviceOrientation else {
        return DispatchQueue.main.sync {
            return previewLayer!.videoPreviewLayer.connection!.videoOrientation
        }
    }

    switch deviceOrientation {
    case .landscapeLeft:
        return .landscapeRight
    case .landscapeRight:
        return .landscapeLeft
    case .portraitUpsideDown:
        return .portraitUpsideDown
    default:
        return .portrait
    }
}

dppeak avatar Oct 02 '17 17:10 dppeak

Please pull this into the main branch.

vindicatesociety avatar Jul 12 '19 21:07 vindicatesociety