Skip to content

Instantly share code, notes, and snippets.

@sam-moshenko
Last active September 16, 2017 12:29
Show Gist options
  • Save sam-moshenko/562ec61431c4a0ebeb68899b4d1b4d26 to your computer and use it in GitHub Desktop.
Save sam-moshenko/562ec61431c4a0ebeb68899b4d1b4d26 to your computer and use it in GitHub Desktop.
A little helper for downloading images
//
// WUImageDownloader.swift
// WorkUp
//
// Created by Simon Lebedev on 9/6/17.
// Copyright © 2017 Simon. All rights reserved.
//
import UIKit
import AlamofireImage
private let downloader = ImageDownloader()
private let downloaderNotCached = ImageDownloader(configuration: URLSessionConfiguration.default,
downloadPrioritization: .fifo, maximumActiveDownloads: 4, imageCache: nil)
//Preferable use this method to download images because it caches images
//except for case where server you use does not provide unique URLs
//for images which were just modified. In this case use downloadImageNotCached
func downloadImage(path: String?, completion: @escaping (UIImage?) -> ()) {
guard let urlRequest = prepareURLRequest(forPath: path, completion: completion) else { return }
downloader.download(urlRequest) { response in
completion(response.result.value)
}
}
func downloadImageNotCached(path: String?, completion: @escaping (UIImage?) -> ()) {
guard let urlRequest = prepareURLRequest(forPath: path, completion: completion) else { return }
downloaderNotCached.download(urlRequest) { response in
completion(response.result.value)
}
}
private func prepareURLRequest(forPath: String?, completion: @escaping (UIImage?) -> ()) -> URLRequest? {
guard let path = forPath else {
completion(nil)
return nil
}
guard let url = URL(string: path) else {
completion(nil)
return nil
}
return URLRequest(url: url)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment