Created
November 25, 2019 07:52
-
-
Save mikehouse/256581fd12624458888d8a85b138e070 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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