Skip to content

Instantly share code, notes, and snippets.

@griffin-stewie
Created June 12, 2018 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save griffin-stewie/a170f5fee6e7bdb7ae600fb17d6d0976 to your computer and use it in GitHub Desktop.
Save griffin-stewie/a170f5fee6e7bdb7ae600fb17d6d0976 to your computer and use it in GitHub Desktop.
import UIKit
func downsample(imageAt imageURL: URL, to pointSize: CGSize, scale: CGFloat = UIScreen.main.nativeScale) -> UIImage {
let imageSourceOptions = [kCGImageSourceShouldCache: false] as CFDictionary
let imageSource = CGImageSourceCreateWithURL(imageURL as CFURL, imageSourceOptions)!
let maxDimensionInPixels = max(pointSize.width, pointSize.height) * scale
let downsampleOptions = [
kCGImageSourceCreateThumbnailFromImageAlways: true,
kCGImageSourceShouldCacheImmediately: true,
kCGImageSourceCreateThumbnailWithTransform: true,
kCGImageSourceThumbnailMaxPixelSize: maxDimensionInPixels
] as CFDictionary
let downsampledImage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, downsampleOptions)!
let thumbnail = UIImage(cgImage: downsampledImage)
return thumbnail
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment