diff options
| author | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2023-04-28 22:23:36 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2023-04-28 22:23:36 +0200 |
| commit | ba4ee0edf2aba19ad73fa53cb01dd0fb9b527526 (patch) | |
| tree | 111357314127f7e05724b570ed9c6aad6f66e3a2 /Sources/Patterns/CGImage+resize.swift | |
| parent | 428153379e8653d38a1e9da441a06f0956492b2b (diff) | |
Use CoreGraphics to draw instead of SwifTUI2.0.0
Diffstat (limited to 'Sources/Patterns/CGImage+resize.swift')
| -rw-r--r-- | Sources/Patterns/CGImage+resize.swift | 19 |
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() + } +} |