Skip to content

Instantly share code, notes, and snippets.

@thexande
Created January 20, 2019 21:25
Show Gist options
  • Select an option

  • Save thexande/ccd4e1fe00a355647b70c8067d82f35b to your computer and use it in GitHub Desktop.

Select an option

Save thexande/ccd4e1fe00a355647b70c8067d82f35b to your computer and use it in GitHub Desktop.
import Foundation
import CoreGraphics
import SDWebImage
final class ImageAspectRatioCache {
var aspectLookup: [URL:CGFloat] = [:]
func produceAspectRatios(for images: [URL], completion: (() -> Void)?) {
let group = DispatchGroup()
for url in images {
group.enter()
SDWebImageDownloader.shared().downloadImage(with: url,
options: [],
progress: nil) { [weak self] (image, _, _, _) in
if let size = image?.size {
self?.aspectLookup[url] = (size.width / size.height)
}
group.leave()
}
}
group.notify(queue: DispatchQueue.global()) {
completion?()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment