mirror of
https://github.com/samsonjs/SwiftyCam.git
synced 2026-04-27 15:07:43 +00:00
Photo flash now works for front facing cameras
This commit is contained in:
parent
c400ae0118
commit
72b852fa4b
1 changed files with 61 additions and 21 deletions
|
|
@ -296,32 +296,38 @@ open class SwiftyCamViewController: UIViewController {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public func takePhoto() {
|
public func takePhoto() {
|
||||||
|
|
||||||
guard videoDevice != nil else {
|
guard let device = videoDevice else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if videoDevice!.hasFlash && currentCamera == .rear && flashEnabled == true /* TODO: Add Support for Retina Flash and add front flash */ {
|
if device.hasFlash == true && flashEnabled == true /* TODO: Add Support for Retina Flash and add front flash */ {
|
||||||
do {
|
changeFlashSettings(device: device, mode: .on)
|
||||||
try videoDevice!.lockForConfiguration()
|
capturePhotoAsyncronously(completionHandler: { (_) in })
|
||||||
videoDevice?.flashMode = .on
|
|
||||||
videoDevice!.unlockForConfiguration()
|
} else if device.hasFlash == false && flashEnabled == true && currentCamera == .front {
|
||||||
} catch {
|
let flashView = UIView(frame: view.frame)
|
||||||
print("[SwiftyCam]: \(error)")
|
flashView.alpha = 0.0
|
||||||
}
|
flashView.backgroundColor = UIColor.white
|
||||||
}
|
view.addSubview(flashView)
|
||||||
|
|
||||||
if let videoConnection = photoFileOutput?.connection(withMediaType: AVMediaTypeVideo) {
|
UIView.animate(withDuration: 0.1, delay: 0.0, options: .curveEaseInOut, animations: {
|
||||||
videoConnection.videoOrientation = AVCaptureVideoOrientation.portrait
|
flashView.alpha = 1.0
|
||||||
photoFileOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: {(sampleBuffer, error) in
|
|
||||||
if (sampleBuffer != nil) {
|
}, completion: { (_) in
|
||||||
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
|
self.capturePhotoAsyncronously(completionHandler: { (success) in
|
||||||
let image = self.processPhoto(imageData!)
|
UIView.animate(withDuration: 0.1, delay: 0.0, options: .curveEaseInOut, animations: {
|
||||||
|
flashView.alpha = 0.0
|
||||||
// Call delegate and return new image
|
}, completion: { (_) in
|
||||||
self.cameraDelegate?.SwiftyCamDidTakePhoto(image)
|
flashView.removeFromSuperview()
|
||||||
}
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
} else {
|
||||||
|
if device.isFlashActive == true {
|
||||||
|
changeFlashSettings(device: device, mode: .off)
|
||||||
|
}
|
||||||
|
capturePhotoAsyncronously(completionHandler: { (_) in })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -636,6 +642,26 @@ open class SwiftyCamViewController: UIViewController {
|
||||||
return image
|
return image
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fileprivate func capturePhotoAsyncronously(completionHandler: @escaping(Bool) -> ()) {
|
||||||
|
if let videoConnection = photoFileOutput?.connection(withMediaType: AVMediaTypeVideo) {
|
||||||
|
videoConnection.videoOrientation = AVCaptureVideoOrientation.portrait
|
||||||
|
photoFileOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: {(sampleBuffer, error) in
|
||||||
|
if (sampleBuffer != nil) {
|
||||||
|
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
|
||||||
|
let image = self.processPhoto(imageData!)
|
||||||
|
|
||||||
|
// Call delegate and return new image
|
||||||
|
self.cameraDelegate?.SwiftyCamDidTakePhoto(image)
|
||||||
|
completionHandler(true)
|
||||||
|
} else {
|
||||||
|
completionHandler(false)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
completionHandler(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Handle pinch gesture
|
/// Handle pinch gesture
|
||||||
|
|
||||||
@objc fileprivate func zoomGesture(pinch: UIPinchGestureRecognizer) {
|
@objc fileprivate func zoomGesture(pinch: UIPinchGestureRecognizer) {
|
||||||
|
|
@ -733,6 +759,8 @@ open class SwiftyCamViewController: UIViewController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get Devices
|
||||||
|
|
||||||
fileprivate class func deviceWithMediaType(_ mediaType: String, preferringPosition position: AVCaptureDevicePosition) -> AVCaptureDevice? {
|
fileprivate class func deviceWithMediaType(_ mediaType: String, preferringPosition position: AVCaptureDevicePosition) -> AVCaptureDevice? {
|
||||||
if let devices = AVCaptureDevice.devices(withMediaType: mediaType) as? [AVCaptureDevice] {
|
if let devices = AVCaptureDevice.devices(withMediaType: mediaType) as? [AVCaptureDevice] {
|
||||||
return devices.filter({ $0.position == position }).first
|
return devices.filter({ $0.position == position }).first
|
||||||
|
|
@ -740,6 +768,18 @@ open class SwiftyCamViewController: UIViewController {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Enable or disable flash for photo
|
||||||
|
|
||||||
|
fileprivate func changeFlashSettings(device: AVCaptureDevice, mode: AVCaptureFlashMode) {
|
||||||
|
do {
|
||||||
|
try device.lockForConfiguration()
|
||||||
|
device.flashMode = mode
|
||||||
|
device.unlockForConfiguration()
|
||||||
|
} catch {
|
||||||
|
print("[SwiftyCam]: \(error)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Enable flash
|
/// Enable flash
|
||||||
|
|
||||||
fileprivate func enableFlash() {
|
fileprivate func enableFlash() {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue