/
UIImage.swift
140 lines (108 loc) · 5.13 KB
/
UIImage.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
import UIKit
extension UIImage {
static let overview = UIImage(named: "overview",
in: .mapboxNavigation,
compatibleWith: nil)!.withRenderingMode(.alwaysTemplate)
static let volumeUp = UIImage(named: "volume_up",
in: .mapboxNavigation,
compatibleWith: nil)!.withRenderingMode(.alwaysTemplate)
static let volumeOff = UIImage(named: "volume_off",
in: .mapboxNavigation,
compatibleWith: nil)!.withRenderingMode(.alwaysTemplate)
static let feedback = UIImage(named: "feedback",
in: .mapboxNavigation,
compatibleWith: nil)!.withRenderingMode(.alwaysTemplate)
convenience init?(color: UIColor, size: CGSize = CGSize(width: 1.0, height: 1.0)) {
let rect = CGRect(origin: .zero, size: size)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
color.setFill()
UIRectFill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
defer { UIGraphicsEndImageContext() }
guard let cgImage = image?.cgImage else { return nil }
self.init(cgImage: cgImage)
}
func withFontSize(font: UIFont,
size: CGSize?) -> UIImage {
let maxHeight = size?.height ?? font.lineHeight * 2
var constrainedSize = self.size
constrainedSize.width = min(constrainedSize.width,
constrainedSize.width * maxHeight / constrainedSize.height)
constrainedSize.height = min(constrainedSize.height,
maxHeight)
let renderer = UIGraphicsImageRenderer(size: constrainedSize)
return renderer.image { _ in
let rect = CGRect(origin: .zero, size: constrainedSize)
draw(in: rect)
}
}
func withCenteredText(_ text: String,
color: UIColor,
font: UIFont,
size: CGSize? = nil) -> UIImage {
let maxHeight = size?.height ?? font.lineHeight * 2
var constrainedSize = self.size
constrainedSize.width = min(constrainedSize.width,
constrainedSize.width * maxHeight / constrainedSize.height)
constrainedSize.height = min(constrainedSize.height,
maxHeight)
let renderer = UIGraphicsImageRenderer(size: constrainedSize)
return renderer.image { _ in
let textStyle = NSMutableParagraphStyle()
textStyle.alignment = .center
let textFontAttributes: [NSAttributedString.Key: Any] = [
.font: font,
.foregroundColor: color,
.paragraphStyle: textStyle
]
let rect = CGRect(origin: .zero, size: constrainedSize)
draw(in: rect)
(text as NSString).draw(in: rect.offsetBy(dx: 0,
dy: (constrainedSize.height - font.lineHeight) / 2).integral,
withAttributes: textFontAttributes)
}
}
func scaled(to: CGSize) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(size, false, scale)
self.draw(in: CGRect(origin: .zero, size: size))
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return scaledImage
}
func tint(_ tintColor: UIColor) -> UIImage {
let imageSize = size
let imageScale = scale
let contextBounds = CGRect(origin: .zero, size: imageSize)
UIGraphicsBeginImageContextWithOptions(imageSize, false, imageScale)
defer { UIGraphicsEndImageContext() }
UIColor.black.setFill()
UIRectFill(contextBounds)
draw(at: .zero)
guard let imageOverBlack = UIGraphicsGetImageFromCurrentImageContext() else { return self }
tintColor.setFill()
UIRectFill(contextBounds)
imageOverBlack.draw(at: .zero, blendMode: .multiply, alpha: 1)
draw(at: .zero, blendMode: .destinationIn, alpha: 1)
guard let finalImage = UIGraphicsGetImageFromCurrentImageContext() else { return self }
return finalImage
}
/// Returns how much bytes the image takes in the memory.
/// - important: Works reliably only for CGImage backed images.
var memoryCost: Int {
let costPerFrame: Int
let framesCount: Int
if let images = images {
framesCount = images.count > 0 ? images.count : 1
}
else {
framesCount = 1
}
if let cgImage = cgImage {
costPerFrame = cgImage.bytesPerRow * cgImage.height
}
else { // fallback to manual estimation
costPerFrame = (Int(scale * size.width)) * (Int(scale * size.height)) * 4 // 4 for 4 RGBA colour components.
}
return costPerFrame * framesCount
}
}