Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vukcevich/9c70c25c11695079cb9adccf4086b6bf to your computer and use it in GitHub Desktop.
Save vukcevich/9c70c25c11695079cb9adccf4086b6bf 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