]> git.r.bdr.sh - rbdr/patterns/blame_incremental - Sources/Patterns/CGImage+resize.swift
Use CoreGraphics to draw instead of SwifTUI
[rbdr/patterns] / Sources / Patterns / CGImage+resize.swift
... / ...
CommitLineData
1import CoreGraphics
2
3extension CGImage {
4 func resize(size:CGSize) -> CGImage? {
5 let width: Int = Int(size.width)
6 let height: Int = Int(size.height)
7
8 let bytesPerPixel = self.bitsPerPixel / self.bitsPerComponent
9 let destBytesPerRow = width * bytesPerPixel
10
11 guard let colorSpace = self.colorSpace else { return nil }
12 guard let context = CGContext(data: nil, width: width, height: height, bitsPerComponent: self.bitsPerComponent, bytesPerRow: destBytesPerRow, space: colorSpace, bitmapInfo: self.alphaInfo.rawValue) else { return nil }
13
14 context.interpolationQuality = .none
15 context.draw(self, in: CGRect(x: 0, y: 0, width: width, height: height))
16
17 return context.makeImage()
18 }
19}