Skip to content

Instantly share code, notes, and snippets.

@mikehouse
Created November 25, 2019 07:52
Show Gist options
  • Save mikehouse/256581fd12624458888d8a85b138e070 to your computer and use it in GitHub Desktop.
Save mikehouse/256581fd12624458888d8a85b138e070 to your computer and use it in GitHub Desktop.
let url = Bundle.main.url(forResource: "cat1", withExtension: "jpg")!
let size: CGSize = CGSize(width: 60, height: 90)
let scale = UIScreen.main.scale
// Bind the URL to the object without decoding (will not use memory yet).
let options = [kCGImageSourceShouldCache:false] as CFDictionary
let source = CGImageSourceCreateWithURL(url as CFURL, options)
let dimension = max(size.width, size.height) * scale
let downsamplingOptions = [
kCGImageSourceCreateThumbnailFromImageAlways: true,
// CoreGraphics decode/cache the data into image memory
// buffer when `CGImageSourceCreateThumbnailAtIndex(...)` called.
kCGImageSourceShouldCacheImmediately: true,
kCGImageSourceCreateThumbnailWithTransform: true,
kCGImageSourceThumbnailMaxPixelSize: dimension
] as CFDictionary
let image = CGImageSourceCreateThumbnailAtIndex(source!, 0, downsamplingOptions)!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment