ImageDataProvider.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // ImageDataProvider.swift
  3. // Kingfisher
  4. //
  5. // Created by onevcat on 2018/11/13.
  6. //
  7. // Copyright (c) 2018 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` memeber, 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. /// The image identifier of this provider instance. For different image loading, you
  35. /// should provide a different `identifier` value. For example, when you want to load
  36. /// an image from a certain URL, use the `absoluteString` property of that URL is
  37. /// generally a good idea.
  38. ///
  39. /// Kingfisher uses this value to identify whether a finished task is the running and
  40. /// being expected one for a view when you use Kingfisher's view extensions methods.
  41. var identifier: String {get }
  42. /// Provides the data which represents image. Kingfisher uses the data you pass in the
  43. /// handler to process images and caches it for later use.
  44. ///
  45. /// - Parameter handler: The handler you should call when you prepared your data.
  46. /// If the data is loaded successfully, call the handler with
  47. /// a `.success` with the data associated. Otherwise, call it
  48. /// with a `.failure` and pass the error.
  49. ///
  50. /// - Note:
  51. /// If the `handler` is called with a `.failure` and error, a `dataProviderError` of
  52. /// `ImageSettingErrorReason` will be finally thrown out to you as the `KingfisherError`
  53. /// accrossing the framework.
  54. func data(handler: @escaping (Result<Data, Error>) -> Void)
  55. }
  56. /// Represents an image data provider for loading from a local file URL on disk.
  57. /// Uses this type for adding a disk image to Kingfisher. Compared to loading it
  58. /// directly, you can get benefit of using Kingfisher's extension methods, as well
  59. /// as applying `ImageProcessor`s and storing the image to `ImageCache` of Kingfisher.
  60. public struct LocalFileImageDataProvider: ImageDataProvider {
  61. /// The file URL from which the image be loaded.
  62. public let fileURL: URL
  63. /// Creates an image data provider by supplying the target local file URL.
  64. ///
  65. /// - Parameters:
  66. /// - fileURL: The file URL from which the image be loaded.
  67. /// - cacheKey: The key is used for caching the image data. By default,
  68. /// the `absoluteString` of `fileURL` is used.
  69. public init(fileURL: URL, cacheKey: String? = nil) {
  70. self.fileURL = fileURL
  71. self.cacheKey = cacheKey ?? fileURL.absoluteString
  72. }
  73. public var cacheKey: String
  74. public func data(handler: (Result<Data, Error>) -> Void) {
  75. handler( Result { try Data(contentsOf: fileURL) } )
  76. }
  77. public var identifier: String { return fileURL.absoluteString }
  78. }
  79. /// Represents an image data provider for loading image from a given Base64 encoded string.
  80. public struct Base64ImageDataProvider: ImageDataProvider {
  81. let base64String: String
  82. /// Creates an umage data provider by supplying the Base64 encoded string.
  83. ///
  84. /// - Parameters:
  85. /// - base64String: The Base64 encoded string for an image.
  86. /// - cacheKey: The key is used for caching the image data. You need a different key for any different image.
  87. public init(base64String: String, cacheKey: String) {
  88. self.base64String = base64String
  89. self.cacheKey = cacheKey
  90. }
  91. public var cacheKey: String
  92. public var identifier: String { return cacheKey }
  93. public func data(handler: (Result<Data, Error>) -> Void) {
  94. let data = Data(base64Encoded: base64String)!
  95. handler(.success(data))
  96. }
  97. }