瀏覽代碼

Converts local resource to file provider

onevcat 6 年之前
父節點
當前提交
b382de4e9a

+ 8 - 0
Kingfisher.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+	<key>IDEDidComputeMac32BitWarning</key>
+	<true/>
+</dict>
+</plist>

+ 1 - 1
Sources/Extensions/ImageView+Kingfisher.swift

@@ -202,7 +202,7 @@ extension KingfisherWrapper where Base: KFCrossPlatformImageView {
         completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
     {
         return setImage(
-            with: resource.map { .network($0) },
+            with: resource?.convertToSource(),
             placeholder: placeholder,
             options: options,
             progressBlock: progressBlock,

+ 2 - 2
Sources/Extensions/NSButton+Kingfisher.swift

@@ -152,7 +152,7 @@ extension KingfisherWrapper where Base: NSButton {
         completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
     {
         return setImage(
-            with: resource.map { .network($0) },
+            with: resource?.convertToSource(),
             placeholder: placeholder,
             options: options,
             progressBlock: progressBlock,
@@ -273,7 +273,7 @@ extension KingfisherWrapper where Base: NSButton {
         completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
     {
         return setAlternateImage(
-            with: resource.map { .network($0) },
+            with: resource?.convertToSource(),
             placeholder: placeholder,
             options: options,
             progressBlock: progressBlock,

+ 2 - 2
Sources/Extensions/UIButton+Kingfisher.swift

@@ -156,7 +156,7 @@ extension KingfisherWrapper where Base: UIButton {
         completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
     {
         return setImage(
-            with: resource.map { Source.network($0) },
+            with: resource?.convertToSource(),
             for: state,
             placeholder: placeholder,
             options: options,
@@ -298,7 +298,7 @@ extension KingfisherWrapper where Base: UIButton {
         completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
     {
         return setBackgroundImage(
-            with: resource.map { .network($0) },
+            with: resource?.convertToSource(),
             for: state,
             placeholder: placeholder,
             options: options,

+ 1 - 1
Sources/Extensions/WKInterfaceImage+Kingfisher.swift

@@ -154,7 +154,7 @@ extension KingfisherWrapper where Base: WKInterfaceImage {
         completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
     {
         return setImage(
-            with: resource.map { .network($0) },
+            with: resource.convertToSource(),
             placeholder: placeholder,
             options: options,
             progressBlock: progressBlock,

+ 12 - 0
Sources/General/ImageSource/ImageDataProvider.swift

@@ -48,6 +48,13 @@ public protocol ImageDataProvider {
     /// `ImageSettingErrorReason` will be finally thrown out to you as the `KingfisherError`
     /// from the framework.
     func data(handler: @escaping (Result<Data, Error>) -> Void)
+
+    /// The content URL represents this provider, if exists.
+    var contentURL: URL? { get }
+}
+
+public extension ImageDataProvider {
+    var contentURL: URL? { return nil }
 }
 
 /// Represents an image data provider for loading from a local file URL on disk.
@@ -82,6 +89,11 @@ public struct LocalFileImageDataProvider: ImageDataProvider {
     public func data(handler: (Result<Data, Error>) -> Void) {
         handler(Result(catching: { try Data(contentsOf: fileURL) }))
     }
+
+    /// The URL of the local file on the disk.
+    public var contentURL: URL? {
+        return fileURL
+    }
 }
 
 /// Represents an image data provider for loading image from a given Base64 encoded string.

+ 12 - 0
Sources/General/ImageSource/Resource.swift

@@ -38,6 +38,18 @@ public protocol Resource {
     var downloadURL: URL { get }
 }
 
+extension Resource {
+
+    /// Converts `self` to a valid `Source` based on its `downloadURL` scheme. A `.provider` with
+    /// `LocalFileImageDataProvider` associated will be returned if the URL points to a local file. Otherwise,
+    /// `.network` is returned.
+    public func convertToSource() -> Source {
+        return downloadURL.isFileURL ?
+            .provider(LocalFileImageDataProvider(fileURL: downloadURL, cacheKey: cacheKey)) :
+            .network(self)
+    }
+}
+
 /// ImageResource is a simple combination of `downloadURL` and `cacheKey`.
 /// When passed to image view set methods, Kingfisher will try to download the target
 /// image from the `downloadURL`, and then store it with the `cacheKey` as the key in cache.

+ 1 - 2
Sources/General/ImageSource/Source.swift

@@ -75,8 +75,7 @@ public enum Source {
     public var url: URL? {
         switch self {
         case .network(let resource): return resource.downloadURL
-        // `ImageDataProvider` does not provide a URL. All it cares is how to get the data back.
-        case .provider(_): return nil
+        case .provider(let provider): return provider.contentURL
         }
     }
 }

+ 1 - 2
Sources/General/KingfisherManager.swift

@@ -150,9 +150,8 @@ public class KingfisherManager {
         downloadTaskUpdated: DownloadTaskUpdatedBlock? = nil,
         completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)?) -> DownloadTask?
     {
-        let source = Source.network(resource)
         return retrieveImage(
-            with: source,
+            with: resource.convertToSource(),
             options: options,
             progressBlock: progressBlock,
             downloadTaskUpdated: downloadTaskUpdated,

+ 1 - 1
Sources/Networking/ImagePrefetcher.swift

@@ -157,7 +157,7 @@ public class ImagePrefetcher: CustomStringConvertible {
         progressBlock: PrefetcherProgressBlock? = nil,
         completionHandler: PrefetcherCompletionHandler? = nil)
     {
-        self.init(sources: resources.map { .network($0) }, options: options)
+        self.init(sources: resources.map { $0.convertToSource() }, options: options)
         self.progressBlock = progressBlock
         self.completionHandler = completionHandler
     }

+ 1 - 2
Sources/SwiftUI/KFImage.swift

@@ -75,8 +75,7 @@ public struct KFImage: SwiftUI.View {
     /// - Parameter options: The options should be applied when loading the image.
     ///                      Some UIKit related options (such as `ImageTransition.flip`) are not supported.
     public init(_ url: URL?, options: KingfisherOptionsInfo? = nil) {
-		let source: Source? = url.map { $0.isFileURL ? Source.provider(LocalFileImageDataProvider(fileURL: $0)) : Source.network($0) }
-        self.init(source: source, options: options)
+        self.init(source: url?.convertToSource(), options: options)
     }
 
     /// Declares the content and behavior of this view.