Created new demo UI and fixed issue with delegate calls not on main queue

This commit is contained in:
Andrew Walz 2017-01-11 18:11:02 -07:00
parent f8376ccec6
commit 29d99fea4e
18 changed files with 72 additions and 56 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 KiB

View file

@ -11,7 +11,7 @@
},
{
"idiom" : "universal",
"filename" : "Flash@3x.png",
"filename" : "flash@3x.png",
"scale" : "3x"
}
],

Binary file not shown.

After

Width:  |  Height:  |  Size: 935 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

View file

@ -6,12 +6,12 @@
},
{
"idiom" : "universal",
"filename" : "Camera@2x.png",
"filename" : "flashOutline@2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "Camera@3x.png",
"filename" : "flashOutline@3x.png",
"scale" : "3x"
}
],

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2 KiB

View file

@ -6,12 +6,12 @@
},
{
"idiom" : "universal",
"filename" : "flipcamera@2x.png",
"filename" : "CameraSwitch@2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "flipCamera@3x.png",
"filename" : "CameraSwitch@3x.png",
"scale" : "3x"
}
],

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.6 KiB

View file

@ -5,7 +5,6 @@
</device>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11757"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
@ -20,41 +19,11 @@
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="SH4-0K-8ra">
<rect key="frame" x="325" y="30" width="30" height="22"/>
<state key="normal" image="Flash"/>
<connections>
<action selector="toggleFlashAction:" destination="BYZ-38-t0r" eventType="touchUpInside" id="j1T-ak-teR"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Bb3-ix-Jd1">
<rect key="frame" x="20" y="28" width="30" height="26"/>
<state key="normal" image="flipCamera"/>
<connections>
<action selector="cameraSwitchAction:" destination="BYZ-38-t0r" eventType="touchUpInside" id="a9E-90-SZM"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="Bb3-ix-Jd1" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" constant="8" symbolic="YES" id="2Fx-Ut-jDO"/>
<constraint firstAttribute="trailingMargin" secondItem="SH4-0K-8ra" secondAttribute="trailing" constant="4" id="AiE-Y6-Sxj"/>
<constraint firstItem="Bb3-ix-Jd1" firstAttribute="centerY" secondItem="SH4-0K-8ra" secondAttribute="centerY" id="Cnv-VO-rR3"/>
<constraint firstItem="Bb3-ix-Jd1" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leadingMargin" constant="4" id="yuS-xl-hcP"/>
</constraints>
</view>
<connections>
<outlet property="flipCameraButton" destination="Bb3-ix-Jd1" id="Q9m-ib-9iQ"/>
<outlet property="toggleFlashButton" destination="SH4-0K-8ra" id="BSt-X8-4Do"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
</scene>
</scenes>
<resources>
<image name="Flash" width="30" height="19"/>
<image name="flipCamera" width="30" height="26"/>
</resources>
</document>

View file

@ -18,14 +18,16 @@ import UIKit
class ViewController: SwiftyCamViewController, SwiftyCamViewControllerDelegate {
@IBOutlet weak var flipCameraButton: UIButton!
@IBOutlet weak var toggleFlashButton: UIButton!
var flipCameraButton: UIButton!
var flashButton: UIButton!
var captureButton: SwiftyRecordButton!
override func viewDidLoad() {
super.viewDidLoad()
cameraDelegate = self
kMaximumVideoDuration = 10.0
addButtons()
}
override var prefersStatusBarHidden: Bool {
@ -34,13 +36,6 @@ class ViewController: SwiftyCamViewController, SwiftyCamViewControllerDelegate {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
captureButton = SwiftyRecordButton(frame: CGRect(x: view.frame.midX - 37.5, y: view.frame.height - 100.0, width: 75.0, height: 75.0))
self.view.addSubview(captureButton)
captureButton.delegate = self
self.view.bringSubview(toFront: captureButton)
self.view.bringSubview(toFront: flipCameraButton)
self.view.bringSubview(toFront: toggleFlashButton)
}
func SwiftyCamDidTakePhoto(_ photo: UIImage) {
@ -49,10 +44,18 @@ class ViewController: SwiftyCamViewController, SwiftyCamViewControllerDelegate {
func SwiftyCamDidBeginRecordingVideo() {
print("Did Begin Recording")
UIView.animate(withDuration: 0.25, animations: {
self.flashButton.alpha = 0.0
self.flipCameraButton.alpha = 0.0
})
}
func SwiftyCamDidFinishRecordingVideo() {
print("Did finish Recording")
UIView.animate(withDuration: 0.25, animations: {
self.flashButton.alpha = 1.0
self.flipCameraButton.alpha = 1.0
})
}
func SwiftyCamDidFinishProcessingVideoAt(_ url: URL) {
@ -71,12 +74,37 @@ class ViewController: SwiftyCamViewController, SwiftyCamViewControllerDelegate {
print(camera)
}
@IBAction func cameraSwitchAction(_ sender: Any) {
@objc private func cameraSwitchAction(_ sender: Any) {
switchCamera()
}
@IBAction func toggleFlashAction(_ sender: Any) {
@objc private func toggleFlashAction(_ sender: Any) {
flashEnabled = !flashEnabled
if flashEnabled == true {
flashButton.setImage(#imageLiteral(resourceName: "flash"), for: UIControlState())
} else {
flashButton.setImage(#imageLiteral(resourceName: "flashOutline"), for: UIControlState())
}
}
private func addButtons() {
captureButton = SwiftyRecordButton(frame: CGRect(x: view.frame.midX - 37.5, y: view.frame.height - 100.0, width: 75.0, height: 75.0))
self.view.addSubview(captureButton)
//self.view.bringSubview(toFront: captureButton)
captureButton.delegate = self
flipCameraButton = UIButton(frame: CGRect(x: view.frame.width / 4 - 15.0, y: view.frame.height - 74.0, width: 30.0, height: 23.0))
flipCameraButton.setImage(#imageLiteral(resourceName: "flipCamera"), for: UIControlState())
flipCameraButton.addTarget(self, action: #selector(cameraSwitchAction(_:)), for: .touchUpInside)
self.view.addSubview(flipCameraButton)
//self.view.bringSubview(toFront: flipCameraButton)
flashButton = UIButton(frame: CGRect(x: view.frame.width * 3 / 4 - 9.0, y: view.frame.height - 77.5, width: 18.0, height: 30.0))
flashButton.setImage(#imageLiteral(resourceName: "flashOutline"), for: UIControlState())
flashButton.addTarget(self, action: #selector(toggleFlashAction(_:)), for: .touchUpInside)
self.view.addSubview(flashButton)
// self.view.bringSubview(toFront: flashButton)
}
}

View file

@ -395,7 +395,9 @@ open class SwiftyCamViewController: UIViewController {
let outputFilePath = (NSTemporaryDirectory() as NSString).appendingPathComponent((outputFileName as NSString).appendingPathExtension("mov")!)
movieFileOutput.startRecording(toOutputFileURL: URL(fileURLWithPath: outputFilePath), recordingDelegate: self)
self.isVideoRecording = true
self.cameraDelegate?.SwiftyCamDidBeginRecordingVideo()
DispatchQueue.main.async {
self.cameraDelegate?.SwiftyCamDidBeginRecordingVideo()
}
}
else {
movieFileOutput.stopRecording()
@ -426,7 +428,9 @@ open class SwiftyCamViewController: UIViewController {
self.flashView?.removeFromSuperview()
})
}
self.cameraDelegate?.SwiftyCamDidFinishRecordingVideo()
DispatchQueue.main.async {
self.cameraDelegate?.SwiftyCamDidFinishRecordingVideo()
}
}
}
@ -463,7 +467,10 @@ open class SwiftyCamViewController: UIViewController {
}
self.addInputs()
self.cameraDelegate?.SwiftyCamDidSwitchCameras(camera: self.currentCamera)
DispatchQueue.main.async {
self.cameraDelegate?.SwiftyCamDidSwitchCameras(camera: self.currentCamera)
}
self.session.startRunning()
}
@ -496,7 +503,10 @@ open class SwiftyCamViewController: UIViewController {
device.exposureMode = AVCaptureExposureMode.continuousAutoExposure
device.unlockForConfiguration()
//Call delegate function and pass in the location of the touch
self.cameraDelegate?.SwiftyCamDidFocusAtPoint(focusPoint: touchPoint.location(in: previewLayer))
DispatchQueue.main.async {
self.cameraDelegate?.SwiftyCamDidFocusAtPoint(focusPoint: touchPoint.location(in: self.previewLayer))
}
}
catch {
// just ignore
@ -691,7 +701,9 @@ open class SwiftyCamViewController: UIViewController {
let image = self.processPhoto(imageData!)
// Call delegate and return new image
self.cameraDelegate?.SwiftyCamDidTakePhoto(image)
DispatchQueue.main.async {
self.cameraDelegate?.SwiftyCamDidTakePhoto(image)
}
completionHandler(true)
} else {
completionHandler(false)
@ -718,7 +730,9 @@ open class SwiftyCamViewController: UIViewController {
captureDevice?.videoZoomFactor = zoomScale
// Call Delegate function with current zoom scale
self.cameraDelegate?.SwiftyCamDidChangeZoomLevel(zoomLevel: zoomScale)
DispatchQueue.main.async {
self.cameraDelegate?.SwiftyCamDidChangeZoomLevel(zoomLevel: self.zoomScale)
}
captureDevice?.unlockForConfiguration()
@ -745,8 +759,11 @@ open class SwiftyCamViewController: UIViewController {
fileprivate func promptToAppSettings() {
guard promptToAppPrivacySettings == true else {
// Do not prompt user
// Ca// delegate function SwiftyCamDidFailCameraPermissionSettings()
self.cameraDelegate?.SwiftyCamDidFailCameraPermissionSettings()
// Call delegate function SwiftyCamDidFailCameraPermissionSettings()
DispatchQueue.main.async {
self.cameraDelegate?.SwiftyCamDidFailCameraPermissionSettings()
}
return
}
@ -939,7 +956,9 @@ extension SwiftyCamViewController : AVCaptureFileOutputRecordingDelegate {
print("[SwiftyCam]: Movie file finishing error: \(error)")
} else {
//Call delegate function with the URL of the outputfile
self.cameraDelegate?.SwiftyCamDidFinishProcessingVideoAt(outputFileURL)
DispatchQueue.main.async {
self.cameraDelegate?.SwiftyCamDidFinishProcessingVideoAt(outputFileURL)
}
}
}
}