CameraViewController.swift 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. //
  2. // CameraViewController.swift
  3. // CameraViewController
  4. //
  5. // Created by Alex Littlejohn.
  6. // Copyright (c) 2016 zero. All rights reserved.
  7. //
  8. // Modified by Kevin Kieffer on 2019/08/06. Changes as follows:
  9. // Update the overlay constraints when rotating, so the overlay is properly positioned and sized
  10. //
  11. // Updated the button constraint calls and removed the rotate animation method which was not working
  12. import UIKit
  13. import AVFoundation
  14. import Photos
  15. public typealias CameraViewCompletion = (UIImage?, PHAsset?) -> Void
  16. public extension CameraViewController {
  17. /// Provides an image picker wrapped inside a UINavigationController instance
  18. class func imagePickerViewController(croppingParameters: CroppingParameters, completion: @escaping CameraViewCompletion) -> UINavigationController {
  19. let imagePicker = PhotoLibraryViewController()
  20. let navigationController = UINavigationController(rootViewController: imagePicker)
  21. navigationController.navigationBar.barTintColor = UIColor.black
  22. navigationController.navigationBar.barStyle = UIBarStyle.black
  23. navigationController.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
  24. imagePicker.onSelectionComplete = { [weak imagePicker] asset in
  25. if let asset = asset {
  26. let confirmController = ConfirmViewController(asset: asset, croppingParameters: croppingParameters)
  27. confirmController.onComplete = { [weak imagePicker] image, asset in
  28. if let image = image, let asset = asset {
  29. completion(image, asset)
  30. } else {
  31. imagePicker?.dismiss(animated: true, completion: nil)
  32. }
  33. }
  34. confirmController.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
  35. confirmController.modalPresentationStyle = .fullScreen
  36. imagePicker?.present(confirmController, animated: true, completion: nil)
  37. } else {
  38. completion(nil, nil)
  39. }
  40. }
  41. return navigationController
  42. }
  43. }
  44. open class CameraViewController: UIViewController {
  45. var didUpdateViews = false
  46. var croppingParameters: CroppingParameters
  47. var animationRunning = false
  48. let allowVolumeButtonCapture: Bool
  49. var lastInterfaceOrientation : UIInterfaceOrientation?
  50. open var onCompletion: CameraViewCompletion?
  51. var volumeControl: VolumeControl?
  52. var animationDuration: TimeInterval = 0.5
  53. var animationSpring: CGFloat = 0.5
  54. var rotateAnimation: UIView.AnimationOptions = .curveLinear
  55. var cameraButtonEdgeConstraint: NSLayoutConstraint?
  56. var cameraButtonGravityConstraint: NSLayoutConstraint?
  57. var closeButtonEdgeConstraint: NSLayoutConstraint?
  58. var closeButtonGravityConstraint: NSLayoutConstraint?
  59. var containerButtonsEdgeOneConstraint: NSLayoutConstraint?
  60. var containerButtonsEdgeTwoConstraint: NSLayoutConstraint?
  61. var containerButtonsGravityConstraint: NSLayoutConstraint?
  62. var swapButtonEdgeOneConstraint: NSLayoutConstraint?
  63. var swapButtonEdgeTwoConstraint: NSLayoutConstraint?
  64. var swapButtonGravityConstraint: NSLayoutConstraint?
  65. var libraryButtonEdgeOneConstraint: NSLayoutConstraint?
  66. var libraryButtonEdgeTwoConstraint: NSLayoutConstraint?
  67. var libraryButtonGravityConstraint: NSLayoutConstraint?
  68. var flashButtonEdgeConstraint: NSLayoutConstraint?
  69. var flashButtonGravityConstraint: NSLayoutConstraint?
  70. var cameraOverlayEdgeOneConstraint: NSLayoutConstraint?
  71. var cameraOverlayEdgeTwoConstraint: NSLayoutConstraint?
  72. var cameraOverlayWidthConstraint: NSLayoutConstraint?
  73. var cameraOverlayCenterConstraint: NSLayoutConstraint?
  74. let cameraView : CameraView = {
  75. let cameraView = CameraView()
  76. cameraView.translatesAutoresizingMaskIntoConstraints = false
  77. return cameraView
  78. }()
  79. let cameraOverlay : CropOverlay = {
  80. let cameraOverlay = CropOverlay()
  81. cameraOverlay.translatesAutoresizingMaskIntoConstraints = false
  82. cameraOverlay.showsButtons = false
  83. return cameraOverlay
  84. }()
  85. let cameraButton : UIButton = {
  86. let button = UIButton(frame: CGRect(x: 0, y: 0, width: 64, height: 64))
  87. button.translatesAutoresizingMaskIntoConstraints = false
  88. button.isEnabled = false
  89. button.setImage(UIImage(named: "cameraButton",
  90. in: CameraGlobals.shared.bundle,
  91. compatibleWith: nil),
  92. for: .normal)
  93. button.setImage(UIImage(named: "cameraButtonHighlighted",
  94. in: CameraGlobals.shared.bundle,
  95. compatibleWith: nil),
  96. for: .highlighted)
  97. return button
  98. }()
  99. let closeButton : UIButton = {
  100. let button = UIButton()
  101. button.translatesAutoresizingMaskIntoConstraints = false
  102. button.setImage(UIImage(named: "closeButton",
  103. in: CameraGlobals.shared.bundle,
  104. compatibleWith: nil),
  105. for: .normal)
  106. return button
  107. }()
  108. let swapButton : UIButton = {
  109. let button = UIButton()
  110. button.translatesAutoresizingMaskIntoConstraints = false
  111. button.setImage(UIImage(named: "swapButton",
  112. in: CameraGlobals.shared.bundle,
  113. compatibleWith: nil),
  114. for: .normal)
  115. return button
  116. }()
  117. let libraryButton : UIButton = {
  118. let button = UIButton()
  119. button.translatesAutoresizingMaskIntoConstraints = false
  120. button.setImage(UIImage(named: "libraryButton",
  121. in: CameraGlobals.shared.bundle,
  122. compatibleWith: nil),
  123. for: .normal)
  124. return button
  125. }()
  126. let flashButton : UIButton = {
  127. let button = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
  128. button.translatesAutoresizingMaskIntoConstraints = false
  129. button.setImage(UIImage(named: "flashAutoIcon",
  130. in: CameraGlobals.shared.bundle,
  131. compatibleWith: nil),
  132. for: .normal)
  133. return button
  134. }()
  135. let containerSwapLibraryButton : UIView = {
  136. let view = UIView()
  137. view.translatesAutoresizingMaskIntoConstraints = false
  138. return view
  139. }()
  140. private let allowsLibraryAccess: Bool
  141. public init(croppingParameters: CroppingParameters = CroppingParameters(),
  142. allowsLibraryAccess: Bool = true,
  143. allowsSwapCameraOrientation: Bool = true,
  144. allowVolumeButtonCapture: Bool = true,
  145. completion: @escaping CameraViewCompletion) {
  146. self.croppingParameters = croppingParameters
  147. self.allowsLibraryAccess = allowsLibraryAccess
  148. self.allowVolumeButtonCapture = allowVolumeButtonCapture
  149. super.init(nibName: nil, bundle: nil)
  150. onCompletion = completion
  151. cameraOverlay.isHidden = !croppingParameters.isEnabled || !croppingParameters.cameraOverlay
  152. cameraOverlay.isUserInteractionEnabled = false
  153. libraryButton.isEnabled = allowsLibraryAccess
  154. libraryButton.isHidden = !allowsLibraryAccess
  155. swapButton.isEnabled = allowsSwapCameraOrientation
  156. swapButton.isHidden = !allowsSwapCameraOrientation
  157. }
  158. required public init?(coder aDecoder: NSCoder) {
  159. fatalError("init(coder:) has not been implemented")
  160. }
  161. open override var prefersStatusBarHidden: Bool {
  162. return true
  163. }
  164. open override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
  165. return UIStatusBarAnimation.slide
  166. }
  167. /**
  168. * Configure the background of the superview to black
  169. * and add the views on this superview. Then, request
  170. * the update of constraints for this superview.
  171. */
  172. open override func loadView() {
  173. super.loadView()
  174. view.backgroundColor = UIColor.black
  175. [cameraView,
  176. cameraOverlay,
  177. cameraButton,
  178. closeButton,
  179. flashButton,
  180. containerSwapLibraryButton].forEach({ view.addSubview($0) })
  181. [swapButton, libraryButton].forEach({ containerSwapLibraryButton.addSubview($0) })
  182. view.setNeedsUpdateConstraints()
  183. }
  184. private func updateOverlayConstraints() {
  185. let portrait = UIApplication.shared.statusBarOrientation.isPortrait
  186. let padding : CGFloat = portrait ? 16.0 : -16.0
  187. removeCameraOverlayEdgesConstraints()
  188. configCameraOverlayEdgeOneContraint(portrait, padding: padding)
  189. configCameraOverlayEdgeTwoConstraint(portrait, padding: padding)
  190. configCameraOverlayWidthConstraint(portrait)
  191. configCameraOverlayCenterConstraint(portrait)
  192. }
  193. /**
  194. * Setup the constraints when the app is starting or rotating
  195. * the screen.
  196. * To avoid the override/conflict of stable constraint, these
  197. * stable constraint are one time configurable.
  198. * Any other dynamic constraint are configurable when the
  199. * device is rotating, based on the device orientation.
  200. */
  201. override open func updateViewConstraints() {
  202. if !didUpdateViews {
  203. configCameraViewConstraints()
  204. didUpdateViews = true
  205. }
  206. let statusBarOrientation = UIApplication.shared.statusBarOrientation
  207. let portrait = statusBarOrientation.isPortrait
  208. removeCameraButtonConstraints()
  209. configCameraButtonEdgeConstraint(statusBarOrientation)
  210. configCameraButtonGravityConstraint(portrait)
  211. removeCloseButtonConstraints()
  212. configCloseButtonEdgeConstraint(statusBarOrientation)
  213. configCloseButtonGravityConstraint(statusBarOrientation)
  214. removeContainerConstraints()
  215. configContainerEdgeConstraint(statusBarOrientation)
  216. configContainerGravityConstraint(statusBarOrientation)
  217. removeSwapButtonConstraints()
  218. configSwapButtonEdgeConstraint(statusBarOrientation)
  219. configSwapButtonGravityConstraint(statusBarOrientation)
  220. removeLibraryButtonConstraints()
  221. configLibraryEdgeButtonConstraint(statusBarOrientation)
  222. configLibraryGravityButtonConstraint(statusBarOrientation)
  223. configFlashEdgeButtonConstraint(statusBarOrientation)
  224. configFlashGravityButtonConstraint(statusBarOrientation)
  225. updateOverlayConstraints()
  226. super.updateViewConstraints()
  227. }
  228. /**
  229. * Add observer to check when the camera has started,
  230. * enable the volume buttons to take the picture,
  231. * configure the actions of the buttons on the screen,
  232. * check the permissions of access of the camera and
  233. * the photo library.
  234. * Configure the camera focus when the application
  235. * start, to avoid any bluried image.
  236. */
  237. open override func viewDidLoad() {
  238. super.viewDidLoad()
  239. setupActions()
  240. checkPermissions()
  241. cameraView.configureFocus()
  242. cameraView.configureZoom()
  243. if let device = AVCaptureDevice.default(for: .video) {
  244. if !device.hasFlash {
  245. flashButton.isEnabled = false
  246. flashButton.isHidden = true
  247. }
  248. }
  249. }
  250. /**
  251. * Start the session of the camera.
  252. */
  253. open override func viewWillAppear(_ animated: Bool) {
  254. super.viewWillAppear(animated)
  255. cameraView.startSession()
  256. addCameraObserver()
  257. addRotateObserver()
  258. if allowVolumeButtonCapture {
  259. setupVolumeControl()
  260. }
  261. }
  262. /**
  263. * Enable the button to take the picture when the
  264. * camera is ready.
  265. */
  266. open override func viewDidAppear(_ animated: Bool) {
  267. super.viewDidAppear(animated)
  268. if cameraView.session?.isRunning == true {
  269. notifyCameraReady()
  270. }
  271. }
  272. open override func viewWillDisappear(_ animated: Bool) {
  273. super.viewWillDisappear(animated)
  274. NotificationCenter.default.removeObserver(self)
  275. volumeControl = nil
  276. }
  277. /**
  278. * This method will disable the rotation of the
  279. */
  280. override open func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  281. super.viewWillTransition(to: size, with: coordinator)
  282. lastInterfaceOrientation = UIApplication.shared.statusBarOrientation
  283. if animationRunning {
  284. return
  285. }
  286. CATransaction.begin()
  287. CATransaction.setDisableActions(true)
  288. coordinator.animate(alongsideTransition: { [weak self] animation in
  289. self?.view.setNeedsUpdateConstraints()
  290. }, completion: { _ in
  291. CATransaction.commit()
  292. })
  293. }
  294. /**
  295. * Observer the camera status, when it is ready,
  296. * it calls the method cameraReady to enable the
  297. * button to take the picture.
  298. */
  299. private func addCameraObserver() {
  300. NotificationCenter.default.addObserver(
  301. self,
  302. selector: #selector(notifyCameraReady),
  303. name: NSNotification.Name.AVCaptureSessionDidStartRunning,
  304. object: nil)
  305. }
  306. /**
  307. * Observer the device orientation to update the
  308. * orientation of CameraView.
  309. */
  310. private func addRotateObserver() {
  311. NotificationCenter.default.addObserver(
  312. self,
  313. selector: #selector(rotateCameraView),
  314. name: UIDevice.orientationDidChangeNotification,
  315. object: nil)
  316. }
  317. @objc internal func notifyCameraReady() {
  318. cameraButton.isEnabled = true
  319. }
  320. /**
  321. * Attach the take of picture for any volume button.
  322. */
  323. private func setupVolumeControl() {
  324. volumeControl = VolumeControl(view: view) { [weak self] _ in
  325. guard let enabled = self?.cameraButton.isEnabled, enabled else {
  326. return
  327. }
  328. self?.capturePhoto()
  329. }
  330. }
  331. /**
  332. * Configure the action for every button on this
  333. * layout.
  334. */
  335. private func setupActions() {
  336. cameraButton.action = { [weak self] in self?.capturePhoto() }
  337. swapButton.action = { [weak self] in self?.swapCamera() }
  338. libraryButton.action = { [weak self] in self?.showLibrary() }
  339. closeButton.action = { [weak self] in self?.close() }
  340. flashButton.action = { [weak self] in self?.toggleFlash() }
  341. }
  342. /**
  343. * Toggle the buttons status, based on the actual
  344. * state of the camera.
  345. */
  346. private func toggleButtons(enabled: Bool) {
  347. [cameraButton,
  348. closeButton,
  349. swapButton,
  350. libraryButton].forEach({ $0.isEnabled = enabled })
  351. }
  352. @objc func rotateCameraView() {
  353. cameraView.rotatePreview()
  354. updateViewConstraints()
  355. }
  356. func setTransform(transform: CGAffineTransform) {
  357. closeButton.transform = transform
  358. swapButton.transform = transform
  359. libraryButton.transform = transform
  360. flashButton.transform = transform
  361. }
  362. /**
  363. * Validate the permissions of the camera and
  364. * library, if the user do not accept these
  365. * permissions, it shows an view that notifies
  366. * the user that it not allow the permissions.
  367. */
  368. private func checkPermissions() {
  369. if AVCaptureDevice.authorizationStatus(for: AVMediaType.video) != .authorized {
  370. AVCaptureDevice.requestAccess(for: AVMediaType.video) { granted in
  371. DispatchQueue.main.async() { [weak self] in
  372. if !granted {
  373. self?.showNoPermissionsView()
  374. }
  375. }
  376. }
  377. }
  378. }
  379. /**
  380. * Generate the view of no permission.
  381. */
  382. private func showNoPermissionsView(library: Bool = false) {
  383. let permissionsView = PermissionsView(frame: view.bounds)
  384. let title: String
  385. let desc: String
  386. if library {
  387. title = localizedString("permissions.library.title")
  388. desc = localizedString("permissions.library.description")
  389. } else {
  390. title = localizedString("permissions.title")
  391. desc = localizedString("permissions.description")
  392. }
  393. permissionsView.configureInView(view, title: title, description: desc, completion: { [weak self] in self?.close() })
  394. }
  395. /**
  396. * This method will be called when the user
  397. * try to take the picture.
  398. * It will lock any button while the shot is
  399. * taken, then, realease the buttons and save
  400. * the picture on the device.
  401. */
  402. internal func capturePhoto() {
  403. guard let output = cameraView.imageOutput,
  404. let connection = output.connection(with: AVMediaType.video) else {
  405. return
  406. }
  407. if connection.isEnabled {
  408. toggleButtons(enabled: false)
  409. cameraView.capturePhoto { [weak self] image in
  410. guard let image = image else {
  411. self?.toggleButtons(enabled: true)
  412. return
  413. }
  414. self?.saveImage(image: image)
  415. }
  416. }
  417. }
  418. internal func saveImage(image: UIImage) {
  419. let spinner = showSpinner()
  420. cameraView.preview.isHidden = true
  421. if allowsLibraryAccess {
  422. _ = SingleImageSaver()
  423. .setImage(image)
  424. .onSuccess { [weak self] asset in
  425. self?.layoutCameraResult(asset: asset)
  426. self?.hideSpinner(spinner)
  427. }
  428. .onFailure { [weak self] error in
  429. self?.toggleButtons(enabled: true)
  430. self?.showNoPermissionsView(library: true)
  431. self?.cameraView.preview.isHidden = false
  432. self?.hideSpinner(spinner)
  433. }
  434. .save()
  435. } else {
  436. layoutCameraResult(uiImage: image)
  437. hideSpinner(spinner)
  438. }
  439. }
  440. internal func close() {
  441. onCompletion?(nil, nil)
  442. onCompletion = nil
  443. }
  444. internal func showLibrary() {
  445. let imagePicker = CameraViewController.imagePickerViewController(croppingParameters: croppingParameters) { [weak self] image, asset in
  446. defer {
  447. self?.dismiss(animated: true, completion: nil)
  448. }
  449. guard let image = image, let asset = asset else {
  450. return
  451. }
  452. self?.onCompletion?(image, asset)
  453. }
  454. present(imagePicker, animated: true) { [weak self] in
  455. self?.cameraView.stopSession()
  456. }
  457. }
  458. internal func toggleFlash() {
  459. cameraView.cycleFlash()
  460. guard let device = cameraView.device else {
  461. return
  462. }
  463. let image = UIImage(named: flashImage(device.flashMode),
  464. in: CameraGlobals.shared.bundle,
  465. compatibleWith: nil)
  466. flashButton.setImage(image, for: .normal)
  467. }
  468. internal func swapCamera() {
  469. cameraView.swapCameraInput()
  470. flashButton.isHidden = cameraView.currentPosition == AVCaptureDevice.Position.front
  471. }
  472. internal func layoutCameraResult(uiImage: UIImage) {
  473. cameraView.stopSession()
  474. startConfirmController(uiImage: uiImage)
  475. toggleButtons(enabled: true)
  476. }
  477. internal func layoutCameraResult(asset: PHAsset) {
  478. cameraView.stopSession()
  479. startConfirmController(asset: asset)
  480. toggleButtons(enabled: true)
  481. }
  482. private func startConfirmController(uiImage: UIImage) {
  483. let confirmViewController = ConfirmViewController(image: uiImage, croppingParameters: croppingParameters)
  484. confirmViewController.onComplete = { [weak self] image, asset in
  485. defer {
  486. //In iOS13, the volume changed notification channel is being called when dismissing this controller
  487. //Since this controller comes back into view momentarily here, before being dismissed, another
  488. //photo is being taken and the camera shutter sound occurs. As a workaround, the volume control
  489. //is deinitialized here to remove the notification channel.
  490. self?.volumeControl = nil
  491. self?.dismiss(animated: true, completion: nil)
  492. }
  493. guard let image = image else {
  494. return
  495. }
  496. self?.onCompletion?(image, asset)
  497. self?.onCompletion = nil
  498. }
  499. confirmViewController.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
  500. confirmViewController.modalPresentationStyle = .fullScreen
  501. present(confirmViewController, animated: true, completion: nil)
  502. }
  503. private func startConfirmController(asset: PHAsset) {
  504. let confirmViewController = ConfirmViewController(asset: asset, croppingParameters: croppingParameters)
  505. confirmViewController.onComplete = { [weak self] image, asset in
  506. defer {
  507. self?.volumeControl = nil
  508. self?.dismiss(animated: true, completion: nil)
  509. }
  510. guard let image = image, let asset = asset else {
  511. return
  512. }
  513. self?.onCompletion?(image, asset)
  514. self?.onCompletion = nil
  515. }
  516. confirmViewController.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
  517. confirmViewController.modalPresentationStyle = .fullScreen
  518. present(confirmViewController, animated: true, completion: nil)
  519. }
  520. private func showSpinner() -> UIActivityIndicatorView {
  521. let spinner = UIActivityIndicatorView()
  522. spinner.style = .white
  523. spinner.center = view.center
  524. spinner.startAnimating()
  525. view.addSubview(spinner)
  526. view.bringSubviewToFront(spinner)
  527. return spinner
  528. }
  529. private func hideSpinner(_ spinner: UIActivityIndicatorView) {
  530. spinner.stopAnimating()
  531. spinner.removeFromSuperview()
  532. }
  533. }