Utilities.swift
3.81 KB
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
//
// ALUtilities.swift
// ALCameraViewController
//
// Created by Alex Littlejohn on 2015/06/25.
// Copyright (c) 2015 zero. All rights reserved.
//
import UIKit
import AVFoundation
internal func radians(_ degrees: Double) -> Double {
return degrees / 180 * M_PI
}
internal func localizedString(_ key: String) -> String {
return NSLocalizedString(key, tableName: CameraGlobals.shared.stringsTable, bundle: CameraGlobals.shared.bundle, comment: key)
}
internal func currentRotation(_ oldOrientation: UIInterfaceOrientation, newOrientation: UIInterfaceOrientation) -> Double {
switch oldOrientation {
case .portrait:
switch newOrientation {
case .landscapeLeft: return 90
case .landscapeRight: return -90
case .portraitUpsideDown: return 180
default: return 0
}
case .landscapeLeft:
switch newOrientation {
case .portrait: return -90
case .landscapeRight: return 180
case .portraitUpsideDown: return 90
default: return 0
}
case .landscapeRight:
switch newOrientation {
case .portrait: return 90
case .landscapeLeft: return 180
case .portraitUpsideDown: return -90
default: return 0
}
default: return 0
}
}
internal func largestPhotoSize() -> CGSize {
let scale = UIScreen.main.scale
let screenSize = UIScreen.main.bounds.size
let size = CGSize(width: screenSize.width * scale, height: screenSize.height * scale)
return size
}
internal func errorWithKey(_ key: String, domain: String) -> NSError {
let errorString = localizedString(key)
let errorInfo = [NSLocalizedDescriptionKey: errorString]
let error = NSError(domain: domain, code: 0, userInfo: errorInfo)
return error
}
internal func normalizedRect(_ rect: CGRect, orientation: UIImageOrientation) -> CGRect {
let normalizedX = rect.origin.x
let normalizedY = rect.origin.y
let normalizedWidth = rect.width
let normalizedHeight = rect.height
var normalizedRect: CGRect
switch orientation {
case .up, .upMirrored:
normalizedRect = CGRect(x: normalizedX, y: normalizedY, width: normalizedWidth, height: normalizedHeight)
case .down, .downMirrored:
normalizedRect = CGRect(x: 1-normalizedX-normalizedWidth, y: 1-normalizedY-normalizedHeight, width: normalizedWidth, height: normalizedHeight)
case .left, .leftMirrored:
normalizedRect = CGRect(x: 1-normalizedY-normalizedHeight, y: normalizedX, width: normalizedHeight, height: normalizedWidth)
case .right, .rightMirrored:
normalizedRect = CGRect(x: normalizedY, y: 1-normalizedX-normalizedWidth, width: normalizedHeight, height: normalizedWidth)
}
return normalizedRect
}
internal func flashImage(_ mode: AVCaptureFlashMode) -> String {
let image: String
switch mode {
case .auto:
image = "flashAutoIcon"
case .on:
image = "flashOnIcon"
case .off:
image = "flashOffIcon"
}
return image
}
struct ScreenSize {
static let SCREEN_WIDTH = UIScreen.main.bounds.size.width
static let SCREEN_HEIGHT = UIScreen.main.bounds.size.height
static let SCREEN_MAX_LENGTH = max(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
}
struct DeviceConfig {
static let SCREEN_MULTIPLIER : CGFloat = {
if UIDevice.current.userInterfaceIdiom == .phone {
switch ScreenSize.SCREEN_MAX_LENGTH {
case 568.0: return 1.5
case 667.0: return 2.0
case 736.0: return 4.0
default: return 1.0
}
} else {
return 1.0
}
}()
}