Skip to content

Commit

Permalink
Orientation of image view fixed to update with device rotation.
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenClarkApps committed May 4, 2018
1 parent efa0da4 commit fea4d71
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
10 changes: 2 additions & 8 deletions QRCodeReader/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13529" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14111" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13527"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<customFonts key="customFonts">
<array key="AvenirNextCondensed.ttc">
<string>AvenirNextCondensed-Medium</string>
</array>
</customFonts>
<scenes>
<!--Code View Controller-->
<scene sceneID="tne-QT-ifu">
Expand Down Expand Up @@ -101,7 +96,6 @@
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="qba-q7-QE8" firstAttribute="top" secondItem="xpT-9d-mEX" secondAttribute="top" id="3ka-63-yVN"/>
<constraint firstItem="zQO-ow-7le" firstAttribute="trailing" secondItem="qba-q7-QE8" secondAttribute="trailing" id="EDr-TR-bKK"/>
Expand Down
34 changes: 34 additions & 0 deletions QRCodeReader/QRScannerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,40 @@ class QRScannerController: UIViewController {

present(alertPrompt, animated: true, completion: nil)
}
private func updatePreviewLayer(layer: AVCaptureConnection, orientation: AVCaptureVideoOrientation) {
layer.videoOrientation = orientation
videoPreviewLayer?.frame = self.view.bounds
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

if let connection = self.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
}
}
}
}

}

Expand Down

0 comments on commit fea4d71

Please sign in to comment.