KFImageOptions.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // KFImageOptions.swift
  3. // Kingfisher
  4. //
  5. // Created by onevcat on 2020/12/20.
  6. //
  7. // Copyright (c) 2020 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. #if canImport(SwiftUI) && canImport(Combine)
  27. import SwiftUI
  28. import Combine
  29. // MARK: - KFImage creating.
  30. @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
  31. extension KFImageProtocol {
  32. /// Creates a `KFImage` for a given `Source`.
  33. /// - Parameters:
  34. /// - source: The `Source` object defines data information from network or a data provider.
  35. /// - isLoaded: Whether the image is loaded or not. This provides a way to inspect the internal loading
  36. /// state. `true` if the image is loaded successfully. Otherwise, `false`. Do not set the
  37. /// wrapped value from outside.
  38. /// - Returns: A `KFImage` for future configuration or embedding to a `SwiftUI.View`.
  39. public static func source(
  40. _ source: Source?, isLoaded: Binding<Bool> = .constant(false)
  41. ) -> Self
  42. {
  43. Self.init(source: source, isLoaded: isLoaded)
  44. }
  45. /// Creates a `KFImage` for a given `Resource`.
  46. /// - Parameters:
  47. /// - source: The `Resource` object defines data information like key or URL.
  48. /// - isLoaded: Whether the image is loaded or not. This provides a way to inspect the internal loading
  49. /// state. `true` if the image is loaded successfully. Otherwise, `false`. Do not set the
  50. /// wrapped value from outside.
  51. /// - Returns: A `KFImage` for future configuration or embedding to a `SwiftUI.View`.
  52. public static func resource(
  53. _ resource: Resource?, isLoaded: Binding<Bool> = .constant(false)
  54. ) -> Self
  55. {
  56. source(resource?.convertToSource(), isLoaded: isLoaded)
  57. }
  58. /// Creates a `KFImage` for a given `URL`.
  59. /// - Parameters:
  60. /// - url: The URL where the image should be downloaded.
  61. /// - cacheKey: The key used to store the downloaded image in cache.
  62. /// If `nil`, the `absoluteString` of `url` is used as the cache key.
  63. /// - isLoaded: Whether the image is loaded or not. This provides a way to inspect the internal loading
  64. /// state. `true` if the image is loaded successfully. Otherwise, `false`. Do not set the
  65. /// wrapped value from outside.
  66. /// - Returns: A `KFImage` for future configuration or embedding to a `SwiftUI.View`.
  67. public static func url(
  68. _ url: URL?, cacheKey: String? = nil, isLoaded: Binding<Bool> = .constant(false)
  69. ) -> Self
  70. {
  71. source(url?.convertToSource(overrideCacheKey: cacheKey), isLoaded: isLoaded)
  72. }
  73. /// Creates a `KFImage` for a given `ImageDataProvider`.
  74. /// - Parameters:
  75. /// - provider: The `ImageDataProvider` object contains information about the data.
  76. /// - isLoaded: Whether the image is loaded or not. This provides a way to inspect the internal loading
  77. /// state. `true` if the image is loaded successfully. Otherwise, `false`. Do not set the
  78. /// wrapped value from outside.
  79. /// - Returns: A `KFImage` for future configuration or embedding to a `SwiftUI.View`.
  80. public static func dataProvider(
  81. _ provider: ImageDataProvider?, isLoaded: Binding<Bool> = .constant(false)
  82. ) -> Self
  83. {
  84. source(provider?.convertToSource(), isLoaded: isLoaded)
  85. }
  86. /// Creates a builder for some given raw data and a cache key.
  87. /// - Parameters:
  88. /// - data: The data object from which the image should be created.
  89. /// - cacheKey: The key used to store the downloaded image in cache.
  90. /// - isLoaded: Whether the image is loaded or not. This provides a way to inspect the internal loading
  91. /// state. `true` if the image is loaded successfully. Otherwise, `false`. Do not set the
  92. /// wrapped value from outside.
  93. /// - Returns: A `KFImage` for future configuration or embedding to a `SwiftUI.View`.
  94. public static func data(
  95. _ data: Data?, cacheKey: String, isLoaded: Binding<Bool> = .constant(false)
  96. ) -> Self
  97. {
  98. if let data = data {
  99. return dataProvider(RawImageDataProvider(data: data, cacheKey: cacheKey), isLoaded: isLoaded)
  100. } else {
  101. return dataProvider(nil, isLoaded: isLoaded)
  102. }
  103. }
  104. }
  105. @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
  106. extension KFImageProtocol {
  107. /// Sets a placeholder `View` which shows when loading the image.
  108. /// - Parameter content: A view that describes the placeholder.
  109. /// - Returns: A `KFImage` view that contains `content` as its placeholder.
  110. public func placeholder<Content: View>(@ViewBuilder _ content: () -> Content) -> Self {
  111. let v = content()
  112. context.placeholder = AnyView(v)
  113. return self
  114. }
  115. /// Sets cancelling the download task bound to `self` when the view disappearing.
  116. /// - Parameter flag: Whether cancel the task or not.
  117. /// - Returns: A `KFImage` view that cancels downloading task when disappears.
  118. public func cancelOnDisappear(_ flag: Bool) -> Self {
  119. context.cancelOnDisappear = flag
  120. return self
  121. }
  122. /// Sets a fade transition for the image task.
  123. /// - Parameter duration: The duration of the fade transition.
  124. /// - Returns: A `KFImage` with changes applied.
  125. ///
  126. /// Kingfisher will use the fade transition to animate the image in if it is downloaded from web.
  127. /// The transition will not happen when the
  128. /// image is retrieved from either memory or disk cache by default. If you need to do the transition even when
  129. /// the image being retrieved from cache, also call `forceRefresh()` on the returned `KFImage`.
  130. public func fade(duration: TimeInterval) -> Self {
  131. context.options.transition = .fade(duration)
  132. return self
  133. }
  134. }
  135. #endif