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
-
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
public private(set) var credential: URLCredential? -
The underlying Alamofire
Managerinstance used to handle all download requests.Declaration
Swift
public let sessionManager: SessionManager
-
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
public class func defaultURLSessionConfiguration() -> URLSessionConfiguration -
Creates a default
URLCachewith common usage parameter values.Declaration
Swift
public class func defaultURLCache() -> URLCache -
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()) -
Initializes the
ImageDownloaderinstance with the given session manager, download prioritization, maximum active download count and image cache.Declaration
Swift
public init( sessionManager: SessionManager, downloadPrioritization: DownloadPrioritization = .fifo, maximumActiveDownloads: Int = 4, imageCache: ImageRequestCache? = AutoPurgingImageCache())
-
Associates an HTTP Basic Auth credential with all future download requests.
Declaration
Swift
public func addAuthentication( user: String, password: String, persistence: URLCredential.Persistence = .forSession) -
Associates the specified credential with all future download requests.
Declaration
Swift
public func addAuthentication(usingCredential credential: URLCredential)
-
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
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
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
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)
View on GitHub
ImageDownloader Class Reference