Created
June 12, 2018 21:47
-
-
Save griffin-stewie/a170f5fee6e7bdb7ae600fb17d6d0976 to your computer and use it in GitHub Desktop.
Create thumbnail image from URL. https://developer.apple.com/videos/play/wwdc2018/219/
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
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