-
-
Save cathandnya/4f1cab7ca722997a0276692b44f50d19 to your computer and use it in GitHub Desktop.
UIImage+Load.swift
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 { | |
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