Skip to content

Commit

Permalink
Merge pull request onevcat#1386 from onevcat/fix/local-resource-conve…
Browse files Browse the repository at this point in the history
…rting

Converts local resource to file provider
  • Loading branch information
onevcat committed Jan 17, 2020
2 parents 1c1e1b3 + b382de4 commit 5450ac7
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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>
2 changes: 1 addition & 1 deletion Sources/Extensions/ImageView+Kingfisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions Sources/Extensions/NSButton+Kingfisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions Sources/Extensions/UIButton+Kingfisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion Sources/Extensions/WKInterfaceImage+Kingfisher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions Sources/General/ImageSource/ImageDataProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions Sources/General/ImageSource/Resource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions Sources/General/ImageSource/Source.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions Sources/General/KingfisherManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion Sources/Networking/ImagePrefetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions Sources/SwiftUI/KFImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 5450ac7

Please sign in to comment.