Created
May 21, 2019 18:05
-
-
Save LucianoPAlmeida/21e5413aa8fe1302d09b3825ded3f7ec to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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