-
Notifications
You must be signed in to change notification settings - Fork 266
/
CameraViewController.swift
797 lines (630 loc) · 31.7 KB
/
CameraViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
//
// CameraViewController.swift
// NextLevel (http://nextlevel.engineering/)
//
// Copyright (c) 2016-present patrick piemonte (http://patrickpiemonte.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import UIKit
import AVFoundation
import Photos
//import NextLevel
class CameraViewController: UIViewController {
// MARK: - UIViewController
override public var prefersStatusBarHidden: Bool {
return true
}
// MARK: - properties
internal var previewView: UIView?
internal var gestureView: UIView?
internal var focusView: FocusIndicatorView?
internal var controlDockView: UIView?
internal var metadataObjectViews: [UIView]?
internal var recordButton: UIImageView?
internal var flipButton: UIButton?
internal var flashButton: UIButton?
internal var saveButton: UIButton?
internal var longPressGestureRecognizer: UILongPressGestureRecognizer?
internal var photoTapGestureRecognizer: UITapGestureRecognizer?
internal var focusTapGestureRecognizer: UITapGestureRecognizer?
internal var flipDoubleTapGestureRecognizer: UITapGestureRecognizer?
internal var _panStartPoint: CGPoint = .zero
internal var _panStartZoom: CGFloat = 0.0
// MARK: - object lifecycle
public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
deinit {
}
// MARK: - view lifecycle
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.black
self.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
let screenBounds = UIScreen.main.bounds
// preview
self.previewView = UIView(frame: screenBounds)
if let previewView = self.previewView {
previewView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
previewView.backgroundColor = UIColor.black
NextLevel.shared.previewLayer.frame = previewView.bounds
previewView.layer.addSublayer(NextLevel.shared.previewLayer)
self.view.addSubview(previewView)
}
self.focusView = FocusIndicatorView(frame: .zero)
// buttons
self.recordButton = UIImageView(image: UIImage(named: "record_button"))
self.longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPressGestureRecognizer(_:)))
if let recordButton = self.recordButton,
let longPressGestureRecognizer = self.longPressGestureRecognizer {
recordButton.isUserInteractionEnabled = true
recordButton.sizeToFit()
longPressGestureRecognizer.delegate = self
longPressGestureRecognizer.minimumPressDuration = 0.05
longPressGestureRecognizer.allowableMovement = 10.0
recordButton.addGestureRecognizer(longPressGestureRecognizer)
}
self.flipButton = UIButton(type: .custom)
if let flipButton = self.flipButton {
flipButton.setImage(UIImage(named: "flip_button"), for: .normal)
flipButton.sizeToFit()
flipButton.addTarget(self, action: #selector(handleFlipButton(_:)), for: .touchUpInside)
}
self.saveButton = UIButton(type: .custom)
if let saveButton = self.saveButton {
saveButton.setImage(UIImage(named: "save_button"), for: .normal)
saveButton.sizeToFit()
saveButton.addTarget(self, action: #selector(handleSaveButton(_:)), for: .touchUpInside)
}
// capture control "dock"
let controlDockHeight = screenBounds.height * 0.2
self.controlDockView = UIView(frame: CGRect(x: 0, y: screenBounds.height - controlDockHeight, width: screenBounds.width, height: controlDockHeight))
if let controlDockView = self.controlDockView {
controlDockView.backgroundColor = UIColor.clear
controlDockView.autoresizingMask = [.flexibleTopMargin]
self.view.addSubview(controlDockView)
if let recordButton = self.recordButton {
recordButton.center = CGPoint(x: controlDockView.bounds.midX, y: controlDockView.bounds.midY)
controlDockView.addSubview(recordButton)
}
if let flipButton = self.flipButton, let recordButton = self.recordButton {
flipButton.center = CGPoint(x: recordButton.center.x + controlDockView.bounds.width * 0.25 + flipButton.bounds.width * 0.5, y: recordButton.center.y)
controlDockView.addSubview(flipButton)
}
if let saveButton = self.saveButton, let recordButton = self.recordButton {
saveButton.center = CGPoint(x: controlDockView.bounds.width * 0.25 - saveButton.bounds.width * 0.5, y: recordButton.center.y)
controlDockView.addSubview(saveButton)
}
}
// gestures
self.gestureView = UIView(frame: screenBounds)
if let gestureView = self.gestureView, let controlDockView = self.controlDockView {
gestureView.autoresizingMask = [.flexibleWidth, .flexibleBottomMargin]
gestureView.frame.size.height -= controlDockView.frame.height
gestureView.backgroundColor = .clear
self.view.addSubview(gestureView)
self.focusTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleFocusTapGestureRecognizer(_:)))
if let focusTapGestureRecognizer = self.focusTapGestureRecognizer {
focusTapGestureRecognizer.delegate = self
focusTapGestureRecognizer.numberOfTapsRequired = 1
gestureView.addGestureRecognizer(focusTapGestureRecognizer)
}
}
// Configure NextLevel by modifying the configuration ivars
let nextLevel = NextLevel.shared
nextLevel.delegate = self
nextLevel.deviceDelegate = self
nextLevel.flashDelegate = self
nextLevel.videoDelegate = self
nextLevel.photoDelegate = self
nextLevel.metadataObjectsDelegate = self
// video configuration
nextLevel.videoConfiguration.preset = AVCaptureSession.Preset.hd1280x720
nextLevel.videoConfiguration.bitRate = 5500000
nextLevel.videoConfiguration.maxKeyFrameInterval = 30
nextLevel.videoConfiguration.profileLevel = AVVideoProfileLevelH264HighAutoLevel
// audio configuration
nextLevel.audioConfiguration.bitRate = 96000
// metadata objects configuration
nextLevel.metadataObjectTypes = [AVMetadataObject.ObjectType.face, AVMetadataObject.ObjectType.qr]
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if NextLevel.authorizationStatus(forMediaType: AVMediaType.video) == .authorized &&
NextLevel.authorizationStatus(forMediaType: AVMediaType.audio) == .authorized {
do {
try NextLevel.shared.start()
} catch {
print("NextLevel, failed to start camera session")
}
} else {
NextLevel.requestAuthorization(forMediaType: AVMediaType.video) { (mediaType, status) in
print("NextLevel, authorization updated for media \(mediaType) status \(status)")
if NextLevel.authorizationStatus(forMediaType: AVMediaType.video) == .authorized &&
NextLevel.authorizationStatus(forMediaType: AVMediaType.audio) == .authorized {
do {
let nextLevel = NextLevel.shared
try nextLevel.start()
} catch {
print("NextLevel, failed to start camera session")
}
} else if status == .notAuthorized {
// gracefully handle when audio/video is not authorized
print("NextLevel doesn't have authorization for audio or video")
}
}
NextLevel.requestAuthorization(forMediaType: AVMediaType.audio) { (mediaType, status) in
print("NextLevel, authorization updated for media \(mediaType) status \(status)")
if NextLevel.authorizationStatus(forMediaType: AVMediaType.video) == .authorized &&
NextLevel.authorizationStatus(forMediaType: AVMediaType.audio) == .authorized {
do {
let nextLevel = NextLevel.shared
try nextLevel.start()
} catch {
print("NextLevel, failed to start camera session")
}
} else if status == .notAuthorized {
// gracefully handle when audio/video is not authorized
print("NextLevel doesn't have authorization for audio or video")
}
}
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NextLevel.shared.stop()
}
}
// MARK: - library
extension CameraViewController {
internal func albumAssetCollection(withTitle title: String) -> PHAssetCollection? {
let predicate = NSPredicate(format: "localizedTitle = %@", title)
let options = PHFetchOptions()
options.predicate = predicate
let result = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: options)
if result.count > 0 {
return result.firstObject
}
return nil
}
}
// MARK: - capture
extension CameraViewController {
internal func startCapture() {
self.photoTapGestureRecognizer?.isEnabled = false
UIView.animate(withDuration: 0.15, delay: 0, options: .curveEaseInOut, animations: {
self.recordButton?.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
}) { (completed: Bool) in
}
NextLevel.shared.record()
}
internal func pauseCapture() {
UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: {
self.recordButton?.transform = .identity
}) { (completed: Bool) in
NextLevel.shared.pause()
}
}
internal func endCapture() {
self.photoTapGestureRecognizer?.isEnabled = true
if let session = NextLevel.shared.session {
if session.clips.count > 1 {
session.mergeClips(usingPreset: AVAssetExportPresetHighestQuality, completionHandler: { (url: URL?, error: Error?) in
if let url = url {
self.saveVideo(withURL: url)
} else if let _ = error {
print("failed to merge clips at the end of capture \(String(describing: error))")
}
})
} else if let lastClipUrl = session.lastClipUrl {
self.saveVideo(withURL: lastClipUrl)
} else if session.currentClipHasStarted {
session.endClip(completionHandler: { (clip, error) in
if error == nil {
self.saveVideo(withURL: (clip?.url)!)
} else {
print("Error saving video: \(error?.localizedDescription ?? "")")
}
})
} else {
// prompt that the video has been saved
let alertController = UIAlertController(title: "Video Capture", message: "Not enough video captured!", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
}
}
internal func authorizePhotoLibaryIfNecessary() {
let authorizationStatus = PHPhotoLibrary.authorizationStatus()
switch authorizationStatus {
case .restricted:
fallthrough
case .denied:
let alertController = UIAlertController(title: "Oh no!", message: "Access denied.", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
break
case .notDetermined:
PHPhotoLibrary.requestAuthorization({ (status) in
if status == .authorized {
} else {
}
})
break
case .authorized:
break
case .limited:
break
@unknown default:
fatalError("unknown authorization type")
}
}
}
// MARK: - media utilities
extension CameraViewController {
internal func saveVideo(withURL url: URL) {
PHPhotoLibrary.shared().performChanges({
let albumAssetCollection = self.albumAssetCollection(withTitle: NextLevelAlbumTitle)
if albumAssetCollection == nil {
let changeRequest = PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: NextLevelAlbumTitle)
let _ = changeRequest.placeholderForCreatedAssetCollection
}}, completionHandler: { (success1: Bool, error1: Error?) in
if let albumAssetCollection = self.albumAssetCollection(withTitle: NextLevelAlbumTitle) {
PHPhotoLibrary.shared().performChanges({
if let assetChangeRequest = PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: url) {
let assetCollectionChangeRequest = PHAssetCollectionChangeRequest(for: albumAssetCollection)
let enumeration: NSArray = [assetChangeRequest.placeholderForCreatedAsset!]
assetCollectionChangeRequest?.addAssets(enumeration)
}
}, completionHandler: { (success2: Bool, error2: Error?) in
if success2 == true {
// prompt that the video has been saved
let alertController = UIAlertController(title: "Video Saved!", message: "Saved to the camera roll.", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
} else {
// prompt that the video has been saved
let alertController = UIAlertController(title: "Oops!", message: "Something failed!", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
})
}
})
}
internal func savePhoto(photoImage: UIImage) {
let NextLevelAlbumTitle = "NextLevel"
PHPhotoLibrary.shared().performChanges({
let albumAssetCollection = self.albumAssetCollection(withTitle: NextLevelAlbumTitle)
if albumAssetCollection == nil {
let changeRequest = PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: NextLevelAlbumTitle)
let _ = changeRequest.placeholderForCreatedAssetCollection
}
}, completionHandler: { (success1: Bool, error1: Error?) in
if success1 == true {
if let albumAssetCollection = self.albumAssetCollection(withTitle: NextLevelAlbumTitle) {
PHPhotoLibrary.shared().performChanges({
let assetChangeRequest = PHAssetChangeRequest.creationRequestForAsset(from: photoImage)
let assetCollectionChangeRequest = PHAssetCollectionChangeRequest(for: albumAssetCollection)
let enumeration: NSArray = [assetChangeRequest.placeholderForCreatedAsset!]
assetCollectionChangeRequest?.addAssets(enumeration)
}, completionHandler: { (success2: Bool, error2: Error?) in
if success2 == true {
let alertController = UIAlertController(title: "Photo Saved!", message: "Saved to the camera roll.", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
})
}
} else if let _ = error1 {
print("failure capturing photo from video frame \(String(describing: error1))")
}
})
}
}
// MARK: - UIButton
extension CameraViewController {
@objc internal func handleFlipButton(_ button: UIButton) {
NextLevel.shared.flipCaptureDevicePosition()
}
internal func handleFlashModeButton(_ button: UIButton) {
}
@objc internal func handleSaveButton(_ button: UIButton) {
self.endCapture()
}
}
// MARK: - UIGestureRecognizerDelegate
extension CameraViewController: UIGestureRecognizerDelegate {
@objc internal func handleLongPressGestureRecognizer(_ gestureRecognizer: UIGestureRecognizer) {
switch gestureRecognizer.state {
case .began:
self.startCapture()
self._panStartPoint = gestureRecognizer.location(in: self.view)
self._panStartZoom = CGFloat(NextLevel.shared.videoZoomFactor)
break
case .changed:
let newPoint = gestureRecognizer.location(in: self.view)
let scale = (self._panStartPoint.y / newPoint.y)
let newZoom = (scale * self._panStartZoom)
NextLevel.shared.videoZoomFactor = Float(newZoom)
break
case .ended:
fallthrough
case .cancelled:
fallthrough
case .failed:
self.pauseCapture()
fallthrough
default:
break
}
}
}
extension CameraViewController {
internal func handlePhotoTapGestureRecognizer(_ gestureRecognizer: UIGestureRecognizer) {
// play system camera shutter sound
AudioServicesPlaySystemSoundWithCompletion(SystemSoundID(1108), nil)
NextLevel.shared.capturePhotoFromVideo()
}
@objc internal func handleFocusTapGestureRecognizer(_ gestureRecognizer: UIGestureRecognizer) {
let tapPoint = gestureRecognizer.location(in: self.previewView)
if let focusView = self.focusView {
var focusFrame = focusView.frame
focusFrame.origin.x = CGFloat((tapPoint.x - (focusFrame.size.width * 0.5)).rounded())
focusFrame.origin.y = CGFloat((tapPoint.y - (focusFrame.size.height * 0.5)).rounded())
focusView.frame = focusFrame
self.previewView?.addSubview(focusView)
focusView.startAnimation()
}
let adjustedPoint = NextLevel.shared.previewLayer.captureDevicePointConverted(fromLayerPoint: tapPoint)
NextLevel.shared.focusExposeAndAdjustWhiteBalance(atAdjustedPoint: adjustedPoint)
}
}
// MARK: - NextLevelDelegate
extension CameraViewController: NextLevelDelegate {
// permission
func nextLevel(_ nextLevel: NextLevel, didUpdateAuthorizationStatus status: NextLevelAuthorizationStatus, forMediaType mediaType: AVMediaType) {
}
// configuration
func nextLevel(_ nextLevel: NextLevel, didUpdateVideoConfiguration videoConfiguration: NextLevelVideoConfiguration) {
}
func nextLevel(_ nextLevel: NextLevel, didUpdateAudioConfiguration audioConfiguration: NextLevelAudioConfiguration) {
}
// session
func nextLevelSessionWillStart(_ nextLevel: NextLevel) {
print("nextLevelSessionWillStart")
}
func nextLevelSessionDidStart(_ nextLevel: NextLevel) {
print("nextLevelSessionDidStart")
}
func nextLevelSessionDidStop(_ nextLevel: NextLevel) {
print("nextLevelSessionDidStop")
}
// interruption
func nextLevelSessionWasInterrupted(_ nextLevel: NextLevel) {
}
func nextLevelSessionInterruptionEnded(_ nextLevel: NextLevel) {
}
// mode
func nextLevelCaptureModeWillChange(_ nextLevel: NextLevel) {
}
func nextLevelCaptureModeDidChange(_ nextLevel: NextLevel) {
}
}
extension CameraViewController: NextLevelPreviewDelegate {
// preview
func nextLevelWillStartPreview(_ nextLevel: NextLevel) {
}
func nextLevelDidStopPreview(_ nextLevel: NextLevel) {
}
}
extension CameraViewController: NextLevelDeviceDelegate {
// position, orientation
func nextLevelDevicePositionWillChange(_ nextLevel: NextLevel) {
}
func nextLevelDevicePositionDidChange(_ nextLevel: NextLevel) {
}
func nextLevel(_ nextLevel: NextLevel, didChangeDeviceOrientation deviceOrientation: NextLevelDeviceOrientation) {
}
// format
func nextLevel(_ nextLevel: NextLevel, didChangeDeviceFormat deviceFormat: AVCaptureDevice.Format) {
}
// aperture
func nextLevel(_ nextLevel: NextLevel, didChangeCleanAperture cleanAperture: CGRect) {
}
// lens
func nextLevel(_ nextLevel: NextLevel, didChangeLensPosition lensPosition: Float) {
}
// focus, exposure, white balance
func nextLevelWillStartFocus(_ nextLevel: NextLevel) {
}
func nextLevelDidStopFocus(_ nextLevel: NextLevel) {
if let focusView = self.focusView {
if focusView.superview != nil {
focusView.stopAnimation()
}
}
}
func nextLevelWillChangeExposure(_ nextLevel: NextLevel) {
}
func nextLevelDidChangeExposure(_ nextLevel: NextLevel) {
if let focusView = self.focusView {
if focusView.superview != nil {
focusView.stopAnimation()
}
}
}
func nextLevelWillChangeWhiteBalance(_ nextLevel: NextLevel) {
}
func nextLevelDidChangeWhiteBalance(_ nextLevel: NextLevel) {
}
}
// MARK: - NextLevelFlashDelegate
extension CameraViewController: NextLevelFlashAndTorchDelegate {
func nextLevelDidChangeFlashMode(_ nextLevel: NextLevel) {
}
func nextLevelDidChangeTorchMode(_ nextLevel: NextLevel) {
}
func nextLevelFlashActiveChanged(_ nextLevel: NextLevel) {
}
func nextLevelTorchActiveChanged(_ nextLevel: NextLevel) {
}
func nextLevelFlashAndTorchAvailabilityChanged(_ nextLevel: NextLevel) {
}
}
// MARK: - NextLevelVideoDelegate
extension CameraViewController: NextLevelVideoDelegate {
// video zoom
func nextLevel(_ nextLevel: NextLevel, didUpdateVideoZoomFactor videoZoomFactor: Float) {
}
// video frame processing
func nextLevel(_ nextLevel: NextLevel, willProcessRawVideoSampleBuffer sampleBuffer: CMSampleBuffer, onQueue queue: DispatchQueue) {
}
func nextLevel(_ nextLevel: NextLevel, willProcessFrame frame: AnyObject, timestamp: TimeInterval, onQueue queue: DispatchQueue) {
}
// enabled by isCustomContextVideoRenderingEnabled
func nextLevel(_ nextLevel: NextLevel, renderToCustomContextWithImageBuffer imageBuffer: CVPixelBuffer, onQueue queue: DispatchQueue) {
}
// video recording session
func nextLevel(_ nextLevel: NextLevel, didSetupVideoInSession session: NextLevelSession) {
}
func nextLevel(_ nextLevel: NextLevel, didSetupAudioInSession session: NextLevelSession) {
}
func nextLevel(_ nextLevel: NextLevel, didStartClipInSession session: NextLevelSession) {
}
func nextLevel(_ nextLevel: NextLevel, didCompleteClip clip: NextLevelClip, inSession session: NextLevelSession) {
}
func nextLevel(_ nextLevel: NextLevel, didAppendVideoSampleBuffer sampleBuffer: CMSampleBuffer, inSession session: NextLevelSession) {
}
func nextLevel(_ nextLevel: NextLevel, didAppendAudioSampleBuffer sampleBuffer: CMSampleBuffer, inSession session: NextLevelSession) {
}
func nextLevel(_ nextLevel: NextLevel, didAppendVideoPixelBuffer pixelBuffer: CVPixelBuffer, timestamp: TimeInterval, inSession session: NextLevelSession) {
}
func nextLevel(_ nextLevel: NextLevel, didSkipVideoPixelBuffer pixelBuffer: CVPixelBuffer, timestamp: TimeInterval, inSession session: NextLevelSession) {
}
func nextLevel(_ nextLevel: NextLevel, didSkipVideoSampleBuffer sampleBuffer: CMSampleBuffer, inSession session: NextLevelSession) {
}
func nextLevel(_ nextLevel: NextLevel, didSkipAudioSampleBuffer sampleBuffer: CMSampleBuffer, inSession session: NextLevelSession) {
}
func nextLevel(_ nextLevel: NextLevel, didCompleteSession session: NextLevelSession) {
// called when a configuration time limit is specified
self.endCapture()
}
// video frame photo
func nextLevel(_ nextLevel: NextLevel, didCompletePhotoCaptureFromVideoFrame photoDict: [String : Any]?) {
if let dictionary = photoDict,
let photoData = dictionary[NextLevelPhotoJPEGKey] as? Data,
let photoImage = UIImage(data: photoData) {
self.savePhoto(photoImage: photoImage)
}
}
}
// MARK: - NextLevelPhotoDelegate
extension CameraViewController: NextLevelPhotoDelegate {
func nextLevel(_ nextLevel: NextLevel, output: AVCapturePhotoOutput, willBeginCaptureFor resolvedSettings: AVCaptureResolvedPhotoSettings, photoConfiguration: NextLevelPhotoConfiguration) {
}
func nextLevel(_ nextLevel: NextLevel, output: AVCapturePhotoOutput, willCapturePhotoFor resolvedSettings: AVCaptureResolvedPhotoSettings, photoConfiguration: NextLevelPhotoConfiguration) {
}
func nextLevel(_ nextLevel: NextLevel, output: AVCapturePhotoOutput, didCapturePhotoFor resolvedSettings: AVCaptureResolvedPhotoSettings, photoConfiguration: NextLevelPhotoConfiguration) {
}
func nextLevel(_ nextLevel: NextLevel, didFinishProcessingPhoto photo: AVCapturePhoto, photoDict: [String : Any], photoConfiguration: NextLevelPhotoConfiguration) {
PHPhotoLibrary.shared().performChanges({
let albumAssetCollection = self.albumAssetCollection(withTitle: NextLevelAlbumTitle)
if albumAssetCollection == nil {
let changeRequest = PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: NextLevelAlbumTitle)
let _ = changeRequest.placeholderForCreatedAssetCollection
}
}, completionHandler: { (success1: Bool, error1: Error?) in
if success1 == true {
if let albumAssetCollection = self.albumAssetCollection(withTitle: NextLevelAlbumTitle) {
PHPhotoLibrary.shared().performChanges({
if let data = photoDict[NextLevelPhotoFileDataKey] as? Data,
let photoImage = UIImage(data: data) {
let assetChangeRequest = PHAssetChangeRequest.creationRequestForAsset(from: photoImage)
let assetCollectionChangeRequest = PHAssetCollectionChangeRequest(for: albumAssetCollection)
let enumeration: NSArray = [assetChangeRequest.placeholderForCreatedAsset!]
assetCollectionChangeRequest?.addAssets(enumeration)
}
}, completionHandler: { (success2: Bool, error2: Error?) in
if success2 == true {
let alertController = UIAlertController(title: "Photo Saved!", message: "Saved to the camera roll.", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
})
}
} else if let _ = error1 {
print("failure capturing photo from video frame \(String(describing: error1))")
}
})
}
func nextLevelDidCompletePhotoCapture(_ nextLevel: NextLevel) {
}
func nextLevel(_ nextLevel: NextLevel, didFinishProcessingPhoto photo: AVCapturePhoto) {
}
}
// MARK: - KVO
private var CameraViewControllerNextLevelCurrentDeviceObserverContext = "CameraViewControllerNextLevelCurrentDeviceObserverContext"
extension CameraViewController {
internal func addKeyValueObservers() {
self.addObserver(self, forKeyPath: "currentDevice", options: [.new], context: &CameraViewControllerNextLevelCurrentDeviceObserverContext)
}
internal func removeKeyValueObservers() {
self.removeObserver(self, forKeyPath: "currentDevice")
}
override public func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if context == &CameraViewControllerNextLevelCurrentDeviceObserverContext {
//self.captureDeviceDidChange()
}
}
}
extension CameraViewController: NextLevelMetadataOutputObjectsDelegate {
func metadataOutputObjects(_ nextLevel: NextLevel, didOutput metadataObjects: [AVMetadataObject]) {
guard let previewView = self.previewView else {
return
}
if let metadataObjectViews = metadataObjectViews {
for view in metadataObjectViews {
view.removeFromSuperview()
}
self.metadataObjectViews = nil
}
self.metadataObjectViews = metadataObjects.map { metadataObject in
let view = UIView(frame: metadataObject.bounds)
view.backgroundColor = UIColor.clear
view.layer.borderColor = UIColor.yellow.cgColor
view.layer.borderWidth = 1
return view
}
if let metadataObjectViews = self.metadataObjectViews {
for view in metadataObjectViews {
previewView.addSubview(view)
}
}
}
}