-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathUIImageView-GeometryConvesion.swift
148 lines (123 loc) · 4.5 KB
/
UIImageView-GeometryConvesion.swift
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
extension UIImageView {
func convertPoint(fromImagePoint imagePoint: CGPoint) -> CGPoint {
guard let imageSize = image?.size else { return CGPoint.zero }
var viewPoint = imagePoint
let viewSize = bounds.size
let ratioX = viewSize.width / imageSize.width
let ratioY = viewSize.height / imageSize.height
switch contentMode {
case .scaleAspectFit: fallthrough
case .scaleAspectFill:
var scale : CGFloat = 0
if contentMode == .scaleAspectFit {
scale = min(ratioX, ratioY)
}
else {
scale = max(ratioX, ratioY)
}
viewPoint.x *= scale
viewPoint.y *= scale
viewPoint.x += (viewSize.width - imageSize.width * scale) / 2.0
viewPoint.y += (viewSize.height - imageSize.height * scale) / 2.0
case .scaleToFill: fallthrough
case .redraw:
viewPoint.x *= ratioX
viewPoint.y *= ratioY
case .center:
viewPoint.x += viewSize.width / 2.0 - imageSize.width / 2.0
viewPoint.y += viewSize.height / 2.0 - imageSize.height / 2.0
case .top:
viewPoint.x += viewSize.width / 2.0 - imageSize.width / 2.0
case .bottom:
viewPoint.x += viewSize.width / 2.0 - imageSize.width / 2.0
viewPoint.y += viewSize.height - imageSize.height
case .left:
viewPoint.y += viewSize.height / 2.0 - imageSize.height / 2.0
case .right:
viewPoint.x += viewSize.width - imageSize.width
viewPoint.y += viewSize.height / 2.0 - imageSize.height / 2.0
case .topRight:
viewPoint.x += viewSize.width - imageSize.width
case .bottomLeft:
viewPoint.y += viewSize.height - imageSize.height
case .bottomRight:
viewPoint.x += viewSize.width - imageSize.width
viewPoint.y += viewSize.height - imageSize.height
case.topLeft: fallthrough
default:
break
}
return viewPoint
}
func convertRect(fromImageRect imageRect: CGRect) -> CGRect {
let imageTopLeft = imageRect.origin
let imageBottomRight = CGPoint(x: imageRect.maxX, y: imageRect.maxY)
let viewTopLeft = convertPoint(fromImagePoint: imageTopLeft)
let viewBottomRight = convertPoint(fromImagePoint: imageBottomRight)
var viewRect : CGRect = .zero
viewRect.origin = viewTopLeft
viewRect.size = CGSize(width: abs(viewBottomRight.x - viewTopLeft.x), height: abs(viewBottomRight.y - viewTopLeft.y))
return viewRect
}
func convertPoint(fromViewPoint viewPoint: CGPoint) -> CGPoint {
guard let imageSize = image?.size else { return CGPoint.zero }
var imagePoint = viewPoint
let viewSize = bounds.size
let ratioX = viewSize.width / imageSize.width
let ratioY = viewSize.height / imageSize.height
switch contentMode {
case .scaleAspectFit: fallthrough
case .scaleAspectFill:
var scale : CGFloat = 0
if contentMode == .scaleAspectFit {
scale = min(ratioX, ratioY)
}
else {
scale = max(ratioX, ratioY)
}
// Remove the x or y margin added in FitMode
imagePoint.x -= (viewSize.width - imageSize.width * scale) / 2.0
imagePoint.y -= (viewSize.height - imageSize.height * scale) / 2.0
imagePoint.x /= scale;
imagePoint.y /= scale;
case .scaleToFill: fallthrough
case .redraw:
imagePoint.x /= ratioX
imagePoint.y /= ratioY
case .center:
imagePoint.x -= (viewSize.width - imageSize.width) / 2.0
imagePoint.y -= (viewSize.height - imageSize.height) / 2.0
case .top:
imagePoint.x -= (viewSize.width - imageSize.width) / 2.0
case .bottom:
imagePoint.x -= (viewSize.width - imageSize.width) / 2.0
imagePoint.y -= (viewSize.height - imageSize.height);
case .left:
imagePoint.y -= (viewSize.height - imageSize.height) / 2.0
case .right:
imagePoint.x -= (viewSize.width - imageSize.width);
imagePoint.y -= (viewSize.height - imageSize.height) / 2.0
case .topRight:
imagePoint.x -= (viewSize.width - imageSize.width);
case .bottomLeft:
imagePoint.y -= (viewSize.height - imageSize.height);
case .bottomRight:
imagePoint.x -= (viewSize.width - imageSize.width)
imagePoint.y -= (viewSize.height - imageSize.height)
case.topLeft: fallthrough
default:
break
}
return imagePoint
}
func convertRect(fromViewRect viewRect : CGRect) -> CGRect {
let viewTopLeft = viewRect.origin
let viewBottomRight = CGPoint(x: viewRect.maxX, y: viewRect.maxY)
let imageTopLeft = convertPoint(fromImagePoint: viewTopLeft)
let imageBottomRight = convertPoint(fromImagePoint: viewBottomRight)
var imageRect : CGRect = .zero
imageRect.origin = imageTopLeft
imageRect.size = CGSize(width: abs(imageBottomRight.x - imageTopLeft.x), height: abs(imageBottomRight.y - imageTopLeft.y))
return imageRect
}
}