Forget the flash/torch nonsense

This commit is contained in:
Sami Samhuri 2023-11-21 16:57:43 -08:00
parent 5b50330d72
commit a81edae04f
13 changed files with 8 additions and 221 deletions

View file

@ -1,6 +1,6 @@
{ {
"info" : { "info" : {
"version" : 1, "author" : "xcode",
"author" : "xcode" "version" : 1
} }
} }

View file

@ -1,22 +0,0 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "flash@2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "flash@3x.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 935 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.4 KiB

View file

@ -1,22 +0,0 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "flashOutline@2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "flashOutline@3x.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 KiB

View file

@ -1,22 +0,0 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "flashauto@2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "flashauto@3x.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.8 KiB

View file

@ -1,11 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r"> <document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="22155" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait"> <device id="retina4_7" orientation="portrait" appearance="light"/>
<adaptation id="fullscreen"/>
</device>
<dependencies> <dependencies>
<deployment identifier="iOS"/> <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="22131"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/> <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies> </dependencies>
<scenes> <scenes>
@ -36,40 +33,26 @@
<action selector="cameraSwitchTapped:" destination="BYZ-38-t0r" eventType="touchUpInside" id="Du6-FK-hjA"/> <action selector="cameraSwitchTapped:" destination="BYZ-38-t0r" eventType="touchUpInside" id="Du6-FK-hjA"/>
</connections> </connections>
</button> </button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="stR-Rn-UdV">
<rect key="frame" x="275" y="594.5" width="18" height="30"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="8dD-oU-QV8"/>
<constraint firstAttribute="width" constant="18" id="rLN-FU-RYb"/>
</constraints>
<state key="normal" image="flashOutline"/>
<connections>
<action selector="toggleFlashTapped:" destination="BYZ-38-t0r" eventType="touchUpInside" id="C5u-0g-Kim"/>
</connections>
</button>
</subviews> </subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/> <color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints> <constraints>
<constraint firstAttribute="bottom" secondItem="hke-fJ-4fX" secondAttribute="bottom" constant="20" symbolic="YES" id="0OM-Pc-Ze1"/> <constraint firstAttribute="bottom" secondItem="hke-fJ-4fX" secondAttribute="bottom" constant="20" symbolic="YES" id="0OM-Pc-Ze1"/>
<constraint firstItem="hke-fJ-4fX" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="22M-gB-uKl"/> <constraint firstItem="hke-fJ-4fX" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="22M-gB-uKl"/>
<constraint firstItem="stR-Rn-UdV" firstAttribute="leading" secondItem="hke-fJ-4fX" secondAttribute="trailing" constant="50" id="4S2-fF-Ta9"/>
<constraint firstItem="hS2-sy-kuv" firstAttribute="centerY" secondItem="hke-fJ-4fX" secondAttribute="centerY" id="AuI-5x-aKM"/> <constraint firstItem="hS2-sy-kuv" firstAttribute="centerY" secondItem="hke-fJ-4fX" secondAttribute="centerY" id="AuI-5x-aKM"/>
<constraint firstItem="hke-fJ-4fX" firstAttribute="leading" secondItem="hS2-sy-kuv" secondAttribute="trailing" constant="50" id="fY6-aF-vVL"/> <constraint firstItem="hke-fJ-4fX" firstAttribute="leading" secondItem="hS2-sy-kuv" secondAttribute="trailing" constant="50" id="fY6-aF-vVL"/>
<constraint firstItem="stR-Rn-UdV" firstAttribute="centerY" secondItem="hke-fJ-4fX" secondAttribute="centerY" id="sJP-2Z-GAn"/>
</constraints> </constraints>
</view> </view>
<connections> <connections>
<outlet property="captureButton" destination="hke-fJ-4fX" id="nng-T2-6kL"/> <outlet property="captureButton" destination="hke-fJ-4fX" id="nng-T2-6kL"/>
<outlet property="flashButton" destination="stR-Rn-UdV" id="WAJ-Ik-VF7"/>
<outlet property="flipCameraButton" destination="hS2-sy-kuv" id="lWF-Wl-Vg1"/> <outlet property="flipCameraButton" destination="hS2-sy-kuv" id="lWF-Wl-Vg1"/>
</connections> </connections>
</viewController> </viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/> <placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects> </objects>
<point key="canvasLocation" x="-26" y="-24"/>
</scene> </scene>
</scenes> </scenes>
<resources> <resources>
<image name="flashOutline" width="18" height="30"/>
<image name="flipCamera" width="30" height="23"/> <image name="flipCamera" width="30" height="23"/>
</resources> </resources>
</document> </document>

View file

@ -21,7 +21,6 @@ class ViewController: SwiftyCamViewController, SwiftyCamViewControllerDelegate {
@IBOutlet weak var captureButton : SwiftyRecordButton! @IBOutlet weak var captureButton : SwiftyRecordButton!
@IBOutlet weak var flipCameraButton : UIButton! @IBOutlet weak var flipCameraButton : UIButton!
@IBOutlet weak var flashButton : UIButton!
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
@ -29,8 +28,6 @@ class ViewController: SwiftyCamViewController, SwiftyCamViewControllerDelegate {
cameraDelegate = self cameraDelegate = self
allowAutoRotate = true allowAutoRotate = true
audioEnabled = true audioEnabled = true
flashMode = .auto
flashButton.setImage(#imageLiteral(resourceName: "flashauto"), for: UIControl.State())
captureButton.buttonEnabled = false captureButton.buttonEnabled = false
} }
@ -100,11 +97,6 @@ class ViewController: SwiftyCamViewController, SwiftyCamViewControllerDelegate {
@IBAction func cameraSwitchTapped(_ sender: Any) { @IBAction func cameraSwitchTapped(_ sender: Any) {
switchCamera() switchCamera()
} }
@IBAction func toggleFlashTapped(_ sender: Any) {
//flashEnabled = !flashEnabled
toggleFlashAnimation()
}
} }
@ -113,14 +105,12 @@ extension ViewController {
fileprivate func hideButtons() { fileprivate func hideButtons() {
UIView.animate(withDuration: 0.25) { UIView.animate(withDuration: 0.25) {
self.flashButton.alpha = 0.0
self.flipCameraButton.alpha = 0.0 self.flipCameraButton.alpha = 0.0
} }
} }
fileprivate func showButtons() { fileprivate func showButtons() {
UIView.animate(withDuration: 0.25) { UIView.animate(withDuration: 0.25) {
self.flashButton.alpha = 1.0
self.flipCameraButton.alpha = 1.0 self.flipCameraButton.alpha = 1.0
} }
} }
@ -143,19 +133,4 @@ extension ViewController {
} }
} }
} }
fileprivate func toggleFlashAnimation() {
//flashEnabled = !flashEnabled
if flashMode == .auto{
flashMode = .on
flashButton.setImage(#imageLiteral(resourceName: "flash"), for: UIControl.State())
}else if flashMode == .on{
flashMode = .off
flashButton.setImage(#imageLiteral(resourceName: "flashOutline"), for: UIControl.State())
}else if flashMode == .off{
flashMode = .auto
flashButton.setImage(#imageLiteral(resourceName: "flashauto"), for: UIControl.State())
}
}
} }

View file

@ -43,28 +43,6 @@ open class SwiftyCamViewController: UIViewController {
} }
} }
public enum FlashMode{
//Return the equivalent AVCaptureDevice.FlashMode
var AVFlashMode: AVCaptureDevice.FlashMode {
switch self {
case .on:
return .on
case .off:
return .off
case .auto:
return .auto
}
}
//Flash mode is set to auto
case auto
//Flash mode is set to on
case on
//Flash mode is set to off
case off
}
/// Enumeration for video quality of the capture session. Corresponds to a AVCaptureSessionPreset /// Enumeration for video quality of the capture session. Corresponds to a AVCaptureSessionPreset
@ -126,9 +104,6 @@ open class SwiftyCamViewController: UIViewController {
public var videoQuality : VideoQuality = .high public var videoQuality : VideoQuality = .high
// Flash Mode
public var flashMode:FlashMode = .off
/// Sets whether Pinch to Zoom is enabled for the capture session /// Sets whether Pinch to Zoom is enabled for the capture session
public var pinchToZoom = true public var pinchToZoom = true
@ -226,10 +201,6 @@ open class SwiftyCamViewController: UIViewController {
fileprivate var beginZoomScale = CGFloat(1.0) fileprivate var beginZoomScale = CGFloat(1.0)
/// Returns true if the torch (flash) is currently enabled
fileprivate var isCameraTorchOn = false
/// Variable to store result of capture session setup /// Variable to store result of capture session setup
fileprivate var setupResult = SessionSetupResult.success fileprivate var setupResult = SessionSetupResult.success
@ -254,10 +225,6 @@ open class SwiftyCamViewController: UIViewController {
fileprivate var previewLayer : PreviewView! fileprivate var previewLayer : PreviewView!
/// UIView for front facing flash
fileprivate var flashView : UIView?
/// Pan Translation /// Pan Translation
fileprivate var previousPanTranslation : CGFloat = 0.0 fileprivate var previousPanTranslation : CGFloat = 0.0
@ -412,9 +379,6 @@ open class SwiftyCamViewController: UIViewController {
self.isSessionRunning = false self.isSessionRunning = false
} }
//Disble flash if it is currently enabled
disableFlash()
// Unsubscribe from device rotation notifications // Unsubscribe from device rotation notifications
rotationCoordinator = nil rotationCoordinator = nil
} }
@ -439,17 +403,6 @@ open class SwiftyCamViewController: UIViewController {
return return
} }
if currentCamera == .rear && flashMode == .on {
enableFlash()
}
if currentCamera == .front && flashMode == .on {
flashView = UIView(frame: view.frame)
flashView?.backgroundColor = UIColor.white
flashView?.alpha = 0.85
previewLayer.addSubview(flashView!)
}
sessionQueue.async { [unowned self] in sessionQueue.async { [unowned self] in
if !movieFileOutput.isRecording { if !movieFileOutput.isRecording {
if UIDevice.current.isMultitaskingSupported { if UIDevice.current.isMultitaskingSupported {
@ -497,15 +450,7 @@ open class SwiftyCamViewController: UIViewController {
if self.isVideoRecording == true { if self.isVideoRecording == true {
self.isVideoRecording = false self.isVideoRecording = false
movieFileOutput!.stopRecording() movieFileOutput!.stopRecording()
disableFlash()
if currentCamera == .front && flashMode == .on && flashView != nil {
UIView.animate(withDuration: 0.1, delay: 0.0, options: .curveEaseInOut, animations: {
self.flashView?.alpha = 0.0
}, completion: { (_) in
self.flashView?.removeFromSuperview()
})
}
DispatchQueue.main.async { DispatchQueue.main.async {
self.cameraDelegate?.swiftyCam(self, didFinishRecordingVideo: self.currentCamera) self.cameraDelegate?.swiftyCam(self, didFinishRecordingVideo: self.currentCamera)
} }
@ -556,9 +501,6 @@ open class SwiftyCamViewController: UIViewController {
self.session.startRunning() self.session.startRunning()
} }
// If flash is enabled, disable it as the torch is needed for front facing camera
disableFlash()
} }
// MARK: Private Functions // MARK: Private Functions
@ -770,53 +712,6 @@ open class SwiftyCamViewController: UIViewController {
?? AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: position) ?? AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: position)
} }
/// Enable flash
fileprivate func enableFlash() {
if self.isCameraTorchOn == false {
toggleFlash()
}
}
/// Disable flash
fileprivate func disableFlash() {
if self.isCameraTorchOn == true {
toggleFlash()
}
}
/// Toggles between enabling and disabling flash
fileprivate func toggleFlash() {
guard self.currentCamera == .rear else {
// Flash is not supported for front facing camera
return
}
let device = AVCaptureDevice.default(for: AVMediaType.video)
// Check if device has a flash
if (device?.hasTorch)! {
do {
try device?.lockForConfiguration()
if (device?.torchMode == AVCaptureDevice.TorchMode.on) {
device?.torchMode = AVCaptureDevice.TorchMode.off
self.isCameraTorchOn = false
} else {
do {
try device?.setTorchModeOn(level: 1.0)
self.isCameraTorchOn = true
} catch {
print("[SwiftyCam]: \(error)")
}
}
device?.unlockForConfiguration()
} catch {
print("[SwiftyCam]: \(error)")
}
}
}
/// Sets whether SwiftyCam should enable background audio from other applications or sources /// Sets whether SwiftyCam should enable background audio from other applications or sources
fileprivate func setBackgroundAudioPreference() { fileprivate func setBackgroundAudioPreference() {