You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
5.7 KiB

  1. //
  2. // ImageDataProvider.swift
  3. // Kingfisher
  4. //
  5. // Created by onevcat on 2018/11/13.
  6. //
  7. // Copyright (c) 2019 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. import Foundation
  27. /// Represents a data provider to provide image data to Kingfisher when setting with
  28. /// `Source.provider` source. Compared to `Source.network` member, it gives a chance
  29. /// to load some image data in your own way, as long as you can provide the data
  30. /// representation for the image.
  31. public protocol ImageDataProvider {
  32. /// The key used in cache.
  33. var cacheKey: String { get }
  34. /// Provides the data which represents image. Kingfisher uses the data you pass in the
  35. /// handler to process images and caches it for later use.
  36. ///
  37. /// - Parameter handler: The handler you should call when you prepared your data.
  38. /// If the data is loaded successfully, call the handler with
  39. /// a `.success` with the data associated. Otherwise, call it
  40. /// with a `.failure` and pass the error.
  41. ///
  42. /// - Note:
  43. /// If the `handler` is called with a `.failure` with error, a `dataProviderError` of
  44. /// `ImageSettingErrorReason` will be finally thrown out to you as the `KingfisherError`
  45. /// from the framework.
  46. func data(handler: @escaping (Result<Data, Error>) -> Void)
  47. /// The content URL represents this provider, if exists.
  48. var contentURL: URL? { get }
  49. }
  50. public extension ImageDataProvider {
  51. var contentURL: URL? { return nil }
  52. }
  53. /// Represents an image data provider for loading from a local file URL on disk.
  54. /// Uses this type for adding a disk image to Kingfisher. Compared to loading it
  55. /// directly, you can get benefit of using Kingfisher's extension methods, as well
  56. /// as applying `ImageProcessor`s and storing the image to `ImageCache` of Kingfisher.
  57. public struct LocalFileImageDataProvider: ImageDataProvider {
  58. // MARK: Public Properties
  59. /// The file URL from which the image be loaded.
  60. public let fileURL: URL
  61. // MARK: Initializers
  62. /// Creates an image data provider by supplying the target local file URL.
  63. ///
  64. /// - Parameters:
  65. /// - fileURL: The file URL from which the image be loaded.
  66. /// - cacheKey: The key is used for caching the image data. By default,
  67. /// the `absoluteString` of `fileURL` is used.
  68. public init(fileURL: URL, cacheKey: String? = nil) {
  69. self.fileURL = fileURL
  70. self.cacheKey = cacheKey ?? fileURL.absoluteString
  71. }
  72. // MARK: Protocol Conforming
  73. /// The key used in cache.
  74. public var cacheKey: String
  75. public func data(handler: (Result<Data, Error>) -> Void) {
  76. handler(Result(catching: { try Data(contentsOf: fileURL) }))
  77. }
  78. /// The URL of the local file on the disk.
  79. public var contentURL: URL? {
  80. return fileURL
  81. }
  82. }
  83. /// Represents an image data provider for loading image from a given Base64 encoded string.
  84. public struct Base64ImageDataProvider: ImageDataProvider {
  85. // MARK: Public Properties
  86. /// The encoded Base64 string for the image.
  87. public let base64String: String
  88. // MARK: Initializers
  89. /// Creates an image data provider by supplying the Base64 encoded string.
  90. ///
  91. /// - Parameters:
  92. /// - base64String: The Base64 encoded string for an image.
  93. /// - cacheKey: The key is used for caching the image data. You need a different key for any different image.
  94. public init(base64String: String, cacheKey: String) {
  95. self.base64String = base64String
  96. self.cacheKey = cacheKey
  97. }
  98. // MARK: Protocol Conforming
  99. /// The key used in cache.
  100. public var cacheKey: String
  101. public func data(handler: (Result<Data, Error>) -> Void) {
  102. let data = Data(base64Encoded: base64String)!
  103. handler(.success(data))
  104. }
  105. }
  106. /// Represents an image data provider for a raw data object.
  107. public struct RawImageDataProvider: ImageDataProvider {
  108. // MARK: Public Properties
  109. /// The raw data object to provide to Kingfisher image loader.
  110. public let data: Data
  111. // MARK: Initializers
  112. /// Creates an image data provider by the given raw `data` value and a `cacheKey` be used in Kingfisher cache.
  113. ///
  114. /// - Parameters:
  115. /// - data: The raw data reprensents an image.
  116. /// - cacheKey: The key is used for caching the image data. You need a different key for any different image.
  117. public init(data: Data, cacheKey: String) {
  118. self.data = data
  119. self.cacheKey = cacheKey
  120. }
  121. // MARK: Protocol Conforming
  122. /// The key used in cache.
  123. public var cacheKey: String
  124. public func data(handler: @escaping (Result<Data, Error>) -> Void) {
  125. handler(.success(data))
  126. }
  127. }