ImageDownloader

public 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.

  • 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 progress handler closure called periodically during an image download.

    Declaration

    Swift

    public typealias ProgressHandler = DataRequest.ProgressHandler
  • The completion handler closure used when an image download completes.

    Declaration

    Swift

    public typealias CompletionHandler = (DataResponse<Image>) -> Void
  • Creates a download request using the internal Alamofire SessionManager instance 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 request inside the request receipt since other callers may be relying on the completion of that request. Instead, you should call cancelRequestForRequestReceipt with the returned request receipt to allow the ImageDownloader to optimize the cancellation on behalf of all active callers.

    Declaration

    Swift

    public func download(
            _ urlRequest: URLRequestConvertible,
            receiptID: String = UUID().uuidString,
            filter: ImageFilter? = nil,
            progress: ProgressHandler? = nil,
            progressQueue: DispatchQueue = DispatchQueue.main,
            completion: CompletionHandler?)
            -> RequestReceipt?
  • Creates a download request using the internal Alamofire SessionManager instance 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 call cancelRequestForRequestReceipt with the returned request receipt to allow the ImageDownloader to optimize the cancellation on behalf of all active callers.

    Declaration

    Swift

    public func download(
            _ urlRequests: [URLRequestConvertible],
            filter: ImageFilter? = nil,
            progress: ProgressHandler? = nil,
            progressQueue: DispatchQueue = DispatchQueue.main,
            completion: CompletionHandler? = nil)
            -> [RequestReceipt]
  • Cancels the request in the receipt by removing the response handler and cancelling the request if necessary.

    If the request is pending in the queue, it will be cancelled if no other response handlers are registered with the request. If the request is currently executing or is already completed, the response handler is removed and will not be called.

    Declaration

    Swift

    public func cancelRequest(with requestReceipt: RequestReceipt)