Awalz / SwiftyCam

A Snapchat Inspired iOS Camera Framework written in Swift
BSD 2-Clause "Simplified" License
2.09k stars 327 forks source link

Preview orientation changes on focus in swift4 #137

Open borkasm opened 6 years ago

borkasm commented 6 years ago

When auto-rotation is disabled in portrait mode, placing device horizontally and tapping somewhere to focus causes preview layer to rotate. I did fix that by replacing this code:

if let connection =  self.previewLayer?.videoPreviewLayer.connection  {

    let currentDevice: UIDevice = UIDevice.current

    let orientation: UIDeviceOrientation = currentDevice.orientation

    let previewLayerConnection : AVCaptureConnection = connection

    if previewLayerConnection.isVideoOrientationSupported {

        switch (orientation) {
        case .portrait: updatePreviewLayer(layer: previewLayerConnection, orientation: .portrait)

            break

        case .landscapeRight: updatePreviewLayer(layer: previewLayerConnection, orientation: .landscapeLeft)

            break

        case .landscapeLeft: updatePreviewLayer(layer: previewLayerConnection, orientation: .landscapeRight)

            break

        case .portraitUpsideDown: updatePreviewLayer(layer: previewLayerConnection, orientation: .portraitUpsideDown)

            break

        default: updatePreviewLayer(layer: previewLayerConnection, orientation: .portrait)

            break
        }
    }
}

with this:

if let previewLayerConnection =  self.previewLayer?.videoPreviewLayer.connection  {
    let orientation = self.orientation.getPreviewLayerOrientation()
    if previewLayerConnection.isVideoOrientationSupported {
        updatePreviewLayer(layer: previewLayerConnection, orientation: orientation)
    }
}
maxmzd commented 6 years ago

Confirmed this fixes the issue. Thanks!