Skip to content

Instantly share code, notes, and snippets.

@cathandnya
Created August 24, 2023 00:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cathandnya/4f1cab7ca722997a0276692b44f50d19 to your computer and use it in GitHub Desktop.
Save cathandnya/4f1cab7ca722997a0276692b44f50d19 to your computer and use it in GitHub Desktop.
UIImage+Load.swift
extension UIImage {
static func load(_ imageURL: URL, maxSize: CGFloat) -> UIImage? {
let imageSourceOptions = [kCGImageSourceShouldCache: false] as CFDictionary
let maxDimensionInPixels: CGFloat = maxSize
let downsampleOptions = [
kCGImageSourceCreateThumbnailFromImageAlways: true,
kCGImageSourceShouldCacheImmediately: true,
kCGImageSourceCreateThumbnailWithTransform: true,
kCGImageSourceThumbnailMaxPixelSize: maxDimensionInPixels] as [CFString : Any] as CFDictionary
guard let imageSource = CGImageSourceCreateWithURL(imageURL as CFURL, imageSourceOptions), let downsampledImage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, downsampleOptions) else {
return nil
}
return UIImage(cgImage: downsampledImage)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment