Skip to content

Instantly share code, notes, and snippets.

@LucianoPAlmeida
Created May 21, 2019 18:05
Show Gist options
  • Save LucianoPAlmeida/21e5413aa8fe1302d09b3825ded3f7ec to your computer and use it in GitHub Desktop.
Save LucianoPAlmeida/21e5413aa8fe1302d09b3825ded3f7ec to your computer and use it in GitHub Desktop.
func downsampledImage(at url: URL, to size: CGSize, scale: CGFloat) -> UIImage? {
guard let imageResource = CGImageSourceCreateWithURL(url as CFURL, [kCGImageSourceShouldCache: false] as CFDictionary) else { return nil }
let maxDimensionPixels = max(size.height, size.width) * scale
let options = [
kCGImageSourceShouldCacheImmediately: true,
kCGImageSourceCreateThumbnailWithTransform: true,
kCGImageSourceCreateThumbnailFromImageAlways: true,
kCGImageSourceThumbnailMaxPixelSize: maxDimensionPixels
] as CFDictionary
guard let cgImage = CGImageSourceCreateThumbnailAtIndex(imageResource, 0, options) else { return nil }
return UIImage(cgImage: cgImage)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment