Skip to content

Instantly share code, notes, and snippets.

@rintoandrews90
Created July 10, 2020 19:46
Show Gist options
  • Save rintoandrews90/37ed0b80e49699f9f1b3f7a761a000f6 to your computer and use it in GitHub Desktop.
Save rintoandrews90/37ed0b80e49699f9f1b3f7a761a000f6 to your computer and use it in GitHub Desktop.
func downsample(imageURL: URL, to pointSize: CGSize, scale: CGFloat) -> UIImage {
let imgeSourceOptions = [kCGImageSourceShouldCache: false] as CFDictionary
let imageSource = CGImageSourceCreateWithURL(imageURL as CFURL, imgeSourceOptions)!
let maxDimensionInPixel = max(pointSize.width, pointSize.height) * scale
let downsampleOptions = [
kCGImageSourceCreateThumbnailFromImageAlways: true,
kCGImageSourceShouldCacheImmediately: true,
kCGImageSourceCreateThumbnailWithTransform: true,
kCGImageSourceThumbnailMaxPixelSize: maxDimensionInPixel
] as CFDictionary
let downsampledImage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, downsampleOptions)!
return UIImage(cgImage: downsampledImage)
}
let image = self?.downsample(imageURL: filename, to: CGSize(width: 50, height: 50), scale: 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment