ImageDownloader
open class ImageDownloader
The ImageDownloader class is responsible for downloading images in parallel on a prioritized queue. Incoming
downloads are added to the front or back of the queue depending on the download prioritization. Each downloaded
image is cached in the underlying NSURLCache as well as the in-memory image cache that supports image filters.
By default, any download request with a cached image equivalent in the image cache will automatically be served the
cached image representation. Additional advanced features include supporting multiple image filters and completion
handlers for a single request.
-
The completion handler closure used when an image download completes.
Declaration
Swift
public typealias CompletionHandler = (AFIDataResponse<Image>) -> Void -
The progress handler closure called periodically during an image download.
Declaration
Swift
public typealias ProgressHandler = DataRequest.ProgressHandler
-
Defines the order prioritization of incoming download requests being inserted into the queue.
- fifo: All incoming downloads are added to the back of the queue.
- lifo: All incoming downloads are added to the front of the queue.
Declaration
Swift
public enum DownloadPrioritization
-
The image cache used to store all downloaded images in.
Declaration
Swift
public let imageCache: ImageRequestCache? -
The credential used for authenticating each download request.
Declaration
Swift
open private(set) var credential: URLCredential? { get } -
Response serializer used to convert the image data to UIImage.
Declaration
Swift
public var imageResponseSerializer: ImageResponseSerializer -
The underlying Alamofire
Sessioninstance used to handle all download requests.Declaration
Swift
public let session: Session
-
The default instance of
ImageDownloaderinitialized with default values.Declaration
Swift
public static let `default`: ImageDownloader -
Creates a default
URLSessionConfigurationwith common usage parameter values.Declaration
Swift
open class func defaultURLSessionConfiguration() -> URLSessionConfigurationReturn Value
The default
URLSessionConfigurationinstance. -
Creates a default
URLCachewith common usage parameter values.Declaration
Swift
open class func defaultURLCache() -> URLCacheReturn Value
The default
URLCacheinstance. -
Initializes the
ImageDownloaderinstance with the given configuration, download prioritization, maximum active download count and image cache.Declaration
Swift
public init(configuration: URLSessionConfiguration = ImageDownloader.defaultURLSessionConfiguration(), downloadPrioritization: DownloadPrioritization = .fifo, maximumActiveDownloads: Int = 4, imageCache: ImageRequestCache? = AutoPurgingImageCache())Parameters
configurationThe
URLSessionConfigurationto use to create the underlying AlamofireSessionManagerinstance.downloadPrioritizationThe download prioritization of the download queue.
.fifoby default.maximumActiveDownloadsThe maximum number of active downloads allowed at any given time.
imageCacheThe image cache used to store all downloaded images in.
Return Value
The new
ImageDownloaderinstance. -
Initializes the
ImageDownloaderinstance with the given session manager, download prioritization, maximum active download count and image cache.Declaration
Swift
public init(session: Session, downloadPrioritization: DownloadPrioritization = .fifo, maximumActiveDownloads: Int = 4, imageCache: ImageRequestCache? = AutoPurgingImageCache())Parameters
sessionThe Alamofire
Sessioninstance to handle all download requests.downloadPrioritizationThe download prioritization of the download queue.
.fifoby default.maximumActiveDownloadsThe maximum number of active downloads allowed at any given time.
imageCacheThe image cache used to store all downloaded images in.
Return Value
The new
ImageDownloaderinstance.
-
Associates an HTTP Basic Auth credential with all future download requests.
Declaration
Swift
open func addAuthentication(user: String, password: String, persistence: URLCredential.Persistence = .forSession)Parameters
userThe user.
passwordThe password.
persistenceThe URL credential persistence.
.forSessionby default. -
Associates the specified credential with all future download requests.
Declaration
Swift
open func addAuthentication(usingCredential credential: URLCredential)Parameters
credentialThe credential.
-
Creates a download request using the internal Alamofire
SessionManagerinstance for the specified URL request.If the same download request is already in the queue or currently being downloaded, the filter and completion handler are appended to the already existing request. Once the request completes, all filters and completion handlers attached to the request are executed in the order they were added. Additionally, any filters attached to the request with the same identifiers are only executed once. The resulting image is then passed into each completion handler paired with the filter.
You should not attempt to directly cancel the
requestinside the request receipt since other callers may be relying on the completion of that request. Instead, you should callcancelRequestForRequestReceiptwith the returned request receipt to allow theImageDownloaderto optimize the cancellation on behalf of all active callers.Declaration
Swift
@discardableResult open func download(_ urlRequest: URLRequestConvertible, cacheKey: String? = nil, receiptID: String = UUID().uuidString, serializer: ImageResponseSerializer? = nil, filter: ImageFilter? = nil, progress: ProgressHandler? = nil, progressQueue: DispatchQueue = DispatchQueue.main, completion: CompletionHandler? = nil) -> RequestReceipt?Parameters
urlRequestThe URL request.
cacheKeyAn optional key used to identify the image in the cache. Defaults to
nil.receiptIDThe
identifierfor theRequestReceiptreturned. Defaults to a new, randomly generated UUID.serializerImage response serializer used to convert the image data to
UIImage. Defaults tonilwhich will fall back to the instanceimageResponseSerializer.filterThe image filter to apply to the image after the download is complete. Defaults to
nil.progressThe closure to be executed periodically during the lifecycle of the request. Defaults to
nil.progressQueueThe dispatch queue to call the progress closure on. Defaults to the main queue.
completionThe closure called when the download request is complete. Defaults to
nil.Return Value
The request receipt for the download request if available.
nilif the image is stored in the image cache and the URL request cache policy allows the cache to be used. -
Creates a download request using the internal Alamofire
SessionManagerinstance for each specified URL request.For each request, if the same download request is already in the queue or currently being downloaded, the filter and completion handler are appended to the already existing request. Once the request completes, all filters and completion handlers attached to the request are executed in the order they were added. Additionally, any filters attached to the request with the same identifiers are only executed once. The resulting image is then passed into each completion handler paired with the filter.
You should not attempt to directly cancel any of the
requests inside the request receipts array since other callers may be relying on the completion of that request. Instead, you should callcancelRequestForRequestReceiptwith the returned request receipt to allow theImageDownloaderto optimize the cancellation on behalf of all active callers.Declaration
Swift
@discardableResult open func download(_ urlRequests: [URLRequestConvertible], filter: ImageFilter? = nil, progress: ProgressHandler? = nil, progressQueue: DispatchQueue = DispatchQueue.main, completion: CompletionHandler? = nil) -> [RequestReceipt]Parameters
urlRequestsThe URL requests.
progressThe closure to be executed periodically during the lifecycle of the request. Defaults to
nil.progressQueueThe dispatch queue to call the progress closure on. Defaults to the main queue.
completionThe closure called when each download request is complete.
Return Value
The request receipts for the download requests if available. If an image is stored in the image cache and the URL request cache policy allows the cache to be used, a receipt will not be returned for that request.
-
Cancels the request contained inside the receipt calls the completion handler with a request cancelled error.
Declaration
Swift
open func cancelRequest(with requestReceipt: RequestReceipt)Parameters
requestReceiptThe request receipt to cancel.
View on GitHub
Install in Dash
ImageDownloader Class Reference