Utilities.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // ALUtilities.swift
  3. // ALCameraViewController
  4. //
  5. // Created by Alex Littlejohn on 2015/06/25.
  6. // Copyright (c) 2015 zero. All rights reserved.
  7. //
  8. import UIKit
  9. import AVFoundation
  10. internal func radians(_ degrees: CGFloat) -> CGFloat {
  11. return degrees / 180 * .pi
  12. }
  13. internal func localizedString(_ key: String) -> String {
  14. var bundle: Bundle {
  15. if Bundle.main.path(forResource: CameraGlobals.shared.stringsTable, ofType: "strings") != nil {
  16. return Bundle.main
  17. }
  18. return CameraGlobals.shared.bundle
  19. }
  20. return NSLocalizedString(key, tableName: CameraGlobals.shared.stringsTable, bundle: bundle, comment: key)
  21. }
  22. internal func currentRotation(_ oldOrientation: UIInterfaceOrientation, newOrientation: UIInterfaceOrientation) -> CGFloat {
  23. switch oldOrientation {
  24. case .portrait:
  25. switch newOrientation {
  26. case .landscapeLeft: return 90
  27. case .landscapeRight: return -90
  28. case .portraitUpsideDown: return 180
  29. default: return 0
  30. }
  31. case .landscapeLeft:
  32. switch newOrientation {
  33. case .portrait: return -90
  34. case .landscapeRight: return 180
  35. case .portraitUpsideDown: return 90
  36. default: return 0
  37. }
  38. case .landscapeRight:
  39. switch newOrientation {
  40. case .portrait: return 90
  41. case .landscapeLeft: return 180
  42. case .portraitUpsideDown: return -90
  43. default: return 0
  44. }
  45. default: return 0
  46. }
  47. }
  48. internal func errorWithKey(_ key: String, domain: String) -> NSError {
  49. let errorString = localizedString(key)
  50. let errorInfo = [NSLocalizedDescriptionKey: errorString]
  51. let error = NSError(domain: domain, code: 0, userInfo: errorInfo)
  52. return error
  53. }
  54. internal func normalizedRect(_ rect: CGRect, orientation: UIImage.Orientation) -> CGRect {
  55. let normalizedX = rect.origin.x
  56. let normalizedY = rect.origin.y
  57. let normalizedWidth = rect.width
  58. let normalizedHeight = rect.height
  59. var normalizedRect: CGRect
  60. switch orientation {
  61. case .up, .upMirrored:
  62. normalizedRect = CGRect(x: normalizedX, y: normalizedY, width: normalizedWidth, height: normalizedHeight)
  63. case .down, .downMirrored:
  64. normalizedRect = CGRect(x: 1-normalizedX-normalizedWidth, y: 1-normalizedY-normalizedHeight, width: normalizedWidth, height: normalizedHeight)
  65. case .left, .leftMirrored:
  66. normalizedRect = CGRect(x: 1-normalizedY-normalizedHeight, y: normalizedX, width: normalizedHeight, height: normalizedWidth)
  67. case .right, .rightMirrored:
  68. normalizedRect = CGRect(x: normalizedY, y: 1-normalizedX-normalizedWidth, width: normalizedHeight, height: normalizedWidth)
  69. @unknown default:
  70. fatalError("unknown orientation")
  71. }
  72. return normalizedRect
  73. }
  74. internal func flashImage(_ mode: AVCaptureDevice.FlashMode) -> String {
  75. let image: String
  76. switch mode {
  77. case .auto:
  78. image = "flashAutoIcon"
  79. case .on:
  80. image = "flashOnIcon"
  81. case .off:
  82. image = "flashOffIcon"
  83. @unknown default:
  84. fatalError("unknown flash mode")
  85. }
  86. return image
  87. }
  88. struct ScreenSize {
  89. static let SCREEN_WIDTH = UIScreen.main.bounds.size.width
  90. static let SCREEN_HEIGHT = UIScreen.main.bounds.size.height
  91. static let SCREEN_MAX_LENGTH = max(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
  92. }
  93. struct DeviceConfig {
  94. static let SCREEN_MULTIPLIER : CGFloat = {
  95. if UIDevice.current.userInterfaceIdiom == .phone {
  96. switch ScreenSize.SCREEN_MAX_LENGTH {
  97. case 568.0: return 1.5
  98. case 667.0: return 2.0
  99. case 736.0: return 4.0
  100. default: return 1.0
  101. }
  102. } else {
  103. return 1.0
  104. }
  105. }()
  106. }