forked from jhinericusername/TheraView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCameraViewController.swift
236 lines (204 loc) · 8.96 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
import UIKit
import Photos
class CameraViewController: UIViewController {
@IBOutlet weak var cameraButton: CustomButton!
@IBOutlet weak var previewImageView: UIImageView!
@IBOutlet weak var toggleCameraButton: UIButton!
@IBOutlet weak var galleryButton: UIButton!
@IBOutlet weak var cameraSelectionButton: UIButton!
@IBOutlet weak var videoCameraButton: UIButton!
@IBOutlet weak var toggleFlashButton: UIButton!
var cameraConfig: CameraConfiguration!
let imagePickerController = UIImagePickerController()
var videoRecordingStarted: Bool = false {
didSet{
if videoRecordingStarted {
self.cameraButton.backgroundColor = UIColor.red
} else {
self.cameraButton.backgroundColor = UIColor.white
}
}
}
func checkPermission(completion: @escaping ()->Void) {
let photoAuthorizationStatus = PHPhotoLibrary.authorizationStatus()
switch photoAuthorizationStatus {
case .authorized:
print("Access is granted by user")
completion()
case .notDetermined:
PHPhotoLibrary.requestAuthorization({
(newStatus) in
print("status is \(newStatus)")
if newStatus == PHAuthorizationStatus.authorized {
/* do stuff here */
completion()
print("success")
}
})
print("It is not determined until now")
case .restricted:
// same same
print("User do not have access to photo album.")
case .denied:
// same same
print("User has denied the permission.")
@unknown default:
break
}
}
fileprivate func registerNotification() {
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(appMovedToBackground), name: NSNotification.Name(rawValue: "App is going background"), object: nil)
notificationCenter.addObserver(self, selector: #selector(appCameToForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
}
@objc func appMovedToBackground() {
if videoRecordingStarted {
videoRecordingStarted = false
self.cameraConfig.stopRecording { (error) in
print(error ?? "Video recording error")
}
}
}
@IBAction func gotoGallery(_ sender: Any) {
checkPermission(completion: {
self.imagePickerController.sourceType = .photoLibrary
self.imagePickerController.delegate = self
self.imagePickerController.mediaTypes = ["public.image", "public.movie"]
self.present(self.imagePickerController, animated: true, completion: nil)
})
}
@objc func appCameToForeground() {
print("app enters foreground")
}
override func viewDidLoad() {
super.viewDidLoad()
self.cameraConfig = CameraConfiguration()
cameraConfig.setup { (error) in
if error != nil {
print(error!.localizedDescription)
}
try? self.cameraConfig.displayPreview(self.previewImageView)
}
self.cameraButton.setImage(#imageLiteral(resourceName: "camera"), for: .normal)
self.cameraButton.tintColor = UIColor.black
self.galleryButton.setImage(#imageLiteral(resourceName: "photo_icon"), for: .normal)
registerNotification()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: animated)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.setNavigationBarHidden(false, animated: animated)
}
@IBAction func toggleFlash(_ sender: Any) {
if cameraConfig.flashMode == .on {
cameraConfig.flashMode = .off
self.toggleFlashButton.setImage(#imageLiteral(resourceName: "flash_off"), for: .normal)
} else if cameraConfig.flashMode == .off {
cameraConfig.flashMode = .on
self.toggleFlashButton.setImage(#imageLiteral(resourceName: "flash_on"), for: .normal)
} else {
cameraConfig.flashMode = .auto
self.toggleFlashButton.setImage(#imageLiteral(resourceName: "flash_auto"), for: .normal)
}
}
@objc fileprivate func showToastForSaved() {
showToast(message: "Saved!", fontSize: 12.0)
}
@objc fileprivate func showToastForRecordingStopped() {
showToast(message: "Recording Stopped", fontSize: 12.0)
}
@objc func image(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
if let error = error {
// we got back an error!
showToast(message: "Could not save!! \n\(error)", fontSize: 12)
} else {
showToast(message: "Saved", fontSize: 12.0)
self.galleryButton.setImage(image, for: .normal)
}
}
@objc func video(_ video: String, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
if let error = error {
// we got back an error!
showToast(message: "Could not save!! \n\(error)", fontSize: 12)
} else {
showToast(message: "Saved", fontSize: 12.0)
}
print(video)
}
@IBAction func didTapOnTakePhotoButton(_ sender: Any) {
if self.cameraConfig.outputType == .photo {
self.cameraConfig.captureImage { (image, error) in
guard let image = image else {
print(error ?? "Image capture error")
return
}
self.previewImageView.image = image
// try? PHPhotoLibrary.shared().performChangesAndWait {
// PHAssetChangeRequest.creationRequestForAsset(from: image)
// }
UIImageWriteToSavedPhotosAlbum(image, self, #selector(self.image(_:didFinishSavingWithError:contextInfo:)), nil)
}
} else {
if videoRecordingStarted {
videoRecordingStarted = false
self.cameraConfig.stopRecording { (error) in
print(error ?? "Video recording error")
}
} else if !videoRecordingStarted {
videoRecordingStarted = true
self.cameraConfig.recordVideo { (url, error) in
guard let url = url else {
print(error ?? "Video recording error")
return
}
UISaveVideoAtPathToSavedPhotosAlbum(url.path, self, #selector(self.video(_:didFinishSavingWithError:contextInfo:)), nil)
}
}
}
}
@IBAction func toggleCamera(_ sender: Any) {
do {
try cameraConfig.switchCameras()
} catch {
print(error.localizedDescription)
}
// switch cameraConfig.currentCameraPosition {
// case .some(.front):
// self.toggleCameraButton.setImage(#imageLiteral(resourceName: "camera_front"), for: .normal)
// case .some(.rear):
// self.toggleCameraButton.setImage(#imageLiteral(resourceName: "camera_rear"), for: .normal)
// default:
// return
// }
}
@IBAction func selectVideoMode(_ sender: Any) {
self.cameraConfig.outputType = .video
self.cameraButton.setImage(#imageLiteral(resourceName: "videocam"), for: .normal)
}
@IBAction func selectCameraMode(_ sender: Any) {
self.cameraConfig.outputType = .photo
self.cameraButton.setImage(#imageLiteral(resourceName: "camera"), for: .normal)
}
}
//Disabling Xcode’s OS-Level Debug Logging
//1- From Xcode menu open: Product > Scheme > Edit Scheme
//2- On your Environment Variables set OS_ACTIVITY_MODE in the value set disable
// this will stop the warning but not the error
extension CameraViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
self.galleryButton.contentMode = .scaleAspectFit
self.galleryButton.setImage( pickedImage, for: .normal)
}
if let videoURL = info[UIImagePickerController.InfoKey.mediaURL] as? URL {
print("videoURL:\(String(describing: videoURL))")
}
self.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
}