aboutsummaryrefslogtreecommitdiff
path: root/Sources/Patterns/CGImage+resize.swift
diff options
context:
space:
mode:
Diffstat (limited to 'Sources/Patterns/CGImage+resize.swift')
-rw-r--r--Sources/Patterns/CGImage+resize.swift19
1 files changed, 19 insertions, 0 deletions
diff --git a/Sources/Patterns/CGImage+resize.swift b/Sources/Patterns/CGImage+resize.swift
new file mode 100644
index 0000000..72ef00e
--- /dev/null
+++ b/Sources/Patterns/CGImage+resize.swift
@@ -0,0 +1,19 @@
+import CoreGraphics
+
+extension CGImage {
+ func resize(size:CGSize) -> CGImage? {
+ let width: Int = Int(size.width)
+ let height: Int = Int(size.height)
+
+ let bytesPerPixel = self.bitsPerPixel / self.bitsPerComponent
+ let destBytesPerRow = width * bytesPerPixel
+
+ guard let colorSpace = self.colorSpace else { return nil }
+ guard let context = CGContext(data: nil, width: width, height: height, bitsPerComponent: self.bitsPerComponent, bytesPerRow: destBytesPerRow, space: colorSpace, bitmapInfo: self.alphaInfo.rawValue) else { return nil }
+
+ context.interpolationQuality = .none
+ context.draw(self, in: CGRect(x: 0, y: 0, width: width, height: height))
+
+ return context.makeImage()
+ }
+}