Created
December 16, 2018 09:32
-
-
Save laevandus/0fe92a3ef34d7b209f3ea0fc9fb901ed 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
extension UIImage { | |
convenience init?(thumbnailOfURL url: URL, size: CGSize, scale: CGFloat) { | |
let options = [kCGImageSourceShouldCache: false] as CFDictionary | |
guard let source = CGImageSourceCreateWithURL(url as CFURL, options) else { return nil } | |
let targetDimension = max(size.width, size.height) * scale | |
let thumbnailOptions = [kCGImageSourceCreateThumbnailFromImageAlways: true, | |
kCGImageSourceCreateThumbnailWithTransform: true, | |
kCGImageSourceShouldCacheImmediately: true, | |
kCGImageSourceThumbnailMaxPixelSize: targetDimension] as CFDictionary | |
guard let thumbnail = CGImageSourceCreateThumbnailAtIndex(source, 0, thumbnailOptions) else { return nil } | |
self.init(cgImage: thumbnail) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment