Skip to content

Instantly share code, notes, and snippets.

@anupamchugh
Last active October 8, 2021 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anupamchugh/75fed37246f77ed68b3061f242847c51 to your computer and use it in GitHub Desktop.
Save anupamchugh/75fed37246f77ed68b3061f242847c51 to your computer and use it in GitHub Desktop.
func resizedImage(for size: CGSize) -> UIImage? {
let image = self.cgImage
let context = CGContext(data: nil,
width: Int(size.width),
height: Int(size.height),
bitsPerComponent: image!.bitsPerComponent,
bytesPerRow: Int(size.width),
space: image?.colorSpace ?? CGColorSpace(name: CGColorSpace.sRGB)!,
bitmapInfo: image!.bitmapInfo.rawValue)
context?.interpolationQuality = .high
context?.draw(image!, in: CGRect(origin: .zero, size: size))
guard let scaledImage = context?.makeImage() else { return nil }
return UIImage(cgImage: scaledImage)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment