Răsfoiți Sursa

Suppress all closure warnings

onevcat 5 ani în urmă
părinte
comite
6036b734c7

+ 21 - 0
Sources/Networking/ImageDownloader.swift

@@ -346,6 +346,27 @@ open class ImageDownloader {
             options: info,
             completionHandler: completionHandler)
     }
+
+    /// Downloads an image with a URL and option.
+    ///
+    /// - Parameters:
+    ///   - url: Target URL.
+    ///   - options: The options could control download behavior. See `KingfisherOptionsInfo`.
+    ///   - completionHandler: Called when the download progress finishes. This block will be called in the queue
+    ///                        defined in `.callbackQueue` in `options` parameter.
+    /// - Returns: A downloading task. You could call `cancel` on it to stop the download task.
+    @discardableResult
+    open func downloadImage(
+        with url: URL,
+        options: KingfisherOptionsInfo? = nil,
+        completionHandler: ((Result<ImageLoadingResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
+    {
+        downloadImage(
+            with: url,
+            options: KingfisherParsedOptionsInfo(options),
+            completionHandler: completionHandler
+        )
+    }
 }
 
 // MARK: Cancelling Task

+ 70 - 0
Sources/Networking/ImagePrefetcher.swift

@@ -138,6 +138,35 @@ public class ImagePrefetcher: CustomStringConvertible {
             completionHandler: completionHandler)
     }
 
+    /// Creates an image prefetcher with an array of URLs.
+    ///
+    /// The prefetcher should be initiated with a list of prefetching targets. The URLs list is immutable.
+    /// After you get a valid `ImagePrefetcher` object, you call `start()` on it to begin the prefetching process.
+    /// The images which are already cached will be skipped without downloading again.
+    ///
+    /// - Parameters:
+    ///   - urls: The URLs which should be prefetched.
+    ///   - options: Options could control some behaviors. See `KingfisherOptionsInfo` for more.
+    ///   - completionHandler: Called when the whole prefetching process finished.
+    ///
+    /// - Note:
+    /// By default, the `ImageDownloader.defaultDownloader` and `ImageCache.defaultCache` will be used as
+    /// the downloader and cache target respectively. You can specify another downloader or cache by using
+    /// a customized `KingfisherOptionsInfo`. Both the progress and completion block will be invoked in
+    /// main thread. The `.callbackQueue` value in `optionsInfo` will be ignored in this method.
+    public convenience init(
+        urls: [URL],
+        options: KingfisherOptionsInfo? = nil,
+        completionHandler: PrefetcherCompletionHandler? = nil)
+    {
+        let resources: [Resource] = urls.map { $0 }
+        self.init(
+            resources: resources,
+            options: options,
+            progressBlock: nil,
+            completionHandler: completionHandler)
+    }
+
     /// Creates an image prefetcher with an array of resources.
     ///
     /// - Parameters:
@@ -162,6 +191,27 @@ public class ImagePrefetcher: CustomStringConvertible {
         self.completionHandler = completionHandler
     }
 
+    /// Creates an image prefetcher with an array of resources.
+    ///
+    /// - Parameters:
+    ///   - resources: The resources which should be prefetched. See `Resource` type for more.
+    ///   - options: Options could control some behaviors. See `KingfisherOptionsInfo` for more.
+    ///   - completionHandler: Called when the whole prefetching process finished.
+    ///
+    /// - Note:
+    /// By default, the `ImageDownloader.defaultDownloader` and `ImageCache.defaultCache` will be used as
+    /// the downloader and cache target respectively. You can specify another downloader or cache by using
+    /// a customized `KingfisherOptionsInfo`. Both the progress and completion block will be invoked in
+    /// main thread. The `.callbackQueue` value in `optionsInfo` will be ignored in this method.
+    public convenience init(
+        resources: [Resource],
+        options: KingfisherOptionsInfo? = nil,
+        completionHandler: PrefetcherCompletionHandler? = nil)
+    {
+        self.init(sources: resources.map { $0.convertToSource() }, options: options)
+        self.completionHandler = completionHandler
+    }
+
     /// Creates an image prefetcher with an array of sources.
     ///
     /// - Parameters:
@@ -185,6 +235,26 @@ public class ImagePrefetcher: CustomStringConvertible {
         self.completionSourceHandler = completionHandler
     }
 
+    /// Creates an image prefetcher with an array of sources.
+    ///
+    /// - Parameters:
+    ///   - sources: The sources which should be prefetched. See `Source` type for more.
+    ///   - options: Options could control some behaviors. See `KingfisherOptionsInfo` for more.
+    ///   - completionHandler: Called when the whole prefetching process finished.
+    ///
+    /// - Note:
+    /// By default, the `ImageDownloader.defaultDownloader` and `ImageCache.defaultCache` will be used as
+    /// the downloader and cache target respectively. You can specify another downloader or cache by using
+    /// a customized `KingfisherOptionsInfo`. Both the progress and completion block will be invoked in
+    /// main thread. The `.callbackQueue` value in `optionsInfo` will be ignored in this method.
+    public convenience init(sources: [Source],
+        options: KingfisherOptionsInfo? = nil,
+        completionHandler: PrefetcherSourceCompletionHandler? = nil)
+    {
+        self.init(sources: sources, options: options)
+        self.completionSourceHandler = completionHandler
+    }
+
     init(sources: [Source], options: KingfisherOptionsInfo?) {
         var options = KingfisherParsedOptionsInfo(options)
         prefetchSources = sources