-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathStaticImageSourceView.swift
More file actions
111 lines (103 loc) · 2.38 KB
/
Copy pathStaticImageSourceView.swift
File metadata and controls
111 lines (103 loc) · 2.38 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
#if canImport(SwiftUI) && canImport(Combine)
import ImageIOSwift
import SwiftUI
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
extension ImageProperties {
var rotateZ: Angle {
switch self.exifOrientation {
case 2:
return .zero
case 3:
return .degrees(180)
case 4:
return .zero
case 5:
return .degrees(90)
case 6:
return .degrees(90)
case 7:
return .degrees(-90)
case 8:
return .degrees(-90)
default: // 1
return .zero
}
}
var scaleX: CGFloat {
switch self.exifOrientation {
case 2:
return -1
case 3:
return 1
case 4:
return 1
case 5:
return -imageSize!.width / imageSize!.height
case 6:
return imageSize!.width / imageSize!.height
case 7:
return -imageSize!.width / imageSize!.height
case 8:
return imageSize!.width / imageSize!.height
default: // 1
return 1
}
}
var scaleY: CGFloat {
switch self.exifOrientation {
case 2:
return 1
case 3:
return 1
case 4:
return -1
case 5:
return imageSize!.height / imageSize!.width
case 6:
return imageSize!.height / imageSize!.width
case 7:
return imageSize!.height / imageSize!.width
case 8:
return imageSize!.height / imageSize!.width
default: // 1
return 1
}
}
}
/// Provides the layout bounds of an image source.
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public struct ImageSourceBase: View {
var properties: ImageProperties
public var body: some View {
Rectangle()
.fill(Color.clear)
.frame(idealWidth: properties.imageSize?.width, idealHeight: properties.imageSize?.height)
}
}
/// Displays a single frame of an image source.
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public struct StaticImageSourceView: View {
public var image: CGImage?
public var properties: ImageProperties
public var label: Text
public init(image: CGImage?, properties: ImageProperties, label: Text) {
self.image = image
self.properties = properties
self.label = label
}
public var body: some View {
return ImageSourceBase(properties: properties)
.overlay(
image.map { Image($0, scale: 1, label: self.label)
.resizable()
.renderingMode(.original)
// adjust based on exif orientation
.rotationEffect(properties.rotateZ)
.scaleEffect(x: properties.scaleX,
y: properties.scaleY,
anchor: .center)
}
)
}
}
#endif