-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathUtils.swift
More file actions
118 lines (98 loc) · 3.86 KB
/
Utils.swift
File metadata and controls
118 lines (98 loc) · 3.86 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
import UIKit
import AVFoundation
class Utils {
// Subscribes target to default NotificationCenter .UIDeviceOrientationDidChange
static func subscribeToDeviceOrientationNotifications(_ target: AnyObject, selector: Selector) {
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
let center = NotificationCenter.default
let name = UIDevice.orientationDidChangeNotification
let selector = selector
center.addObserver(target, selector: selector, name: name, object: nil)
}
static func unsubscribeFromOrientationNotifications(_ target: AnyObject) {
let center = NotificationCenter.default
center.removeObserver(target)
//UIDevice.current.endGeneratingDeviceOrientationNotifications()
}
static func videoOrientationFromDeviceOrientation(
videoOrientation: AVCaptureVideoOrientation) -> AVCaptureVideoOrientation {
let deviceOrientation = UIDevice.current.orientation
switch deviceOrientation {
case .unknown:
return videoOrientation
case .portrait:
// Device oriented vertically, home button on the bottom
return .portrait
case .portraitUpsideDown:
// Device oriented vertically, home button on the top
return .portraitUpsideDown
case .landscapeLeft:
// Device oriented horizontally, home button on the right
return .landscapeRight
case .landscapeRight:
// Device oriented horizontally, home button on the left
return .landscapeLeft
case .faceUp:
// Device oriented flat, face up
return videoOrientation
case .faceDown:
// Device oriented flat, face down
return videoOrientation
@unknown default:
fatalError()
}
}
static func exifOrientationFromDeviceOrientation() -> CGImagePropertyOrientation {
let deviceOrientation = UIDevice.current.orientation
switch deviceOrientation {
case .portraitUpsideDown: // Device oriented vertically, home button on the top
return .left
case .landscapeLeft: // Device oriented horizontally, home button on the right
return .upMirrored
case .landscapeRight: // Device oriented horizontally, home button on the left
return .down
case .portrait: // Device oriented vertically, home button on the bottom
return .up
default:
return .up
}
}
static func imageOrientationFromInterfaceOrientation() -> UIImage.Orientation {
let interfaceOrientation = UIApplication.shared.statusBarOrientation
switch interfaceOrientation {
case .portrait:
return .right
case .portraitUpsideDown:
return .left
case .landscapeRight:
return .up
case .landscapeLeft:
return .down
default:
return .right
}
}
static func contentModeFromInterfaceOrientation(for image: UIImage) -> UIView.ContentMode {
let interfaceOrientation = UIApplication.shared.statusBarOrientation
let imageOrientation = image.imageOrientation
switch (interfaceOrientation, imageOrientation) {
case (.portrait, .right),
(.portrait, .left),
(.portraitUpsideDown, .left),
(.portraitUpsideDown, .right),
(.landscapeLeft, .up),
(.landscapeLeft, .down),
(.landscapeRight, .up),
(.landscapeRight, .down):
return .scaleAspectFill
default:
return .scaleAspectFit
}
}
static var isIPhoneX: Bool {
if UIDevice.current.userInterfaceIdiom == .phone {
return UIScreen.main.nativeBounds.height >= 1792 // iPhone XR
}
return false
}
}