KFImageOptions.swift 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 Kingfisher-compatible image view with a given ``Source``.
  33. ///
  34. /// - Parameters:
  35. /// - source: The ``Source`` object that defines data information from the network or a data provider.
  36. /// - Returns: A Kingfisher-compatible image view for future configuration or embedding into another `SwiftUI.View`.
  37. public static func source(
  38. _ source: Source?
  39. ) -> Self
  40. {
  41. Self.init(source: source)
  42. }
  43. /// Creates a Kingfisher-compatible image view with a given ``Resource``.
  44. ///
  45. /// - Parameters:
  46. /// - resource: The ``Resource`` object that defines data information such as a key or URL.
  47. /// - Returns: A Kingfisher-compatible image view for future configuration or embedding into another `SwiftUI.View`.
  48. public static func resource(
  49. _ resource: Resource?
  50. ) -> Self
  51. {
  52. source(resource?.convertToSource())
  53. }
  54. /// Creates a Kingfisher-compatible image view with a given `URL`.
  55. ///
  56. /// - Parameters:
  57. /// - url: The `URL` from which the image should be downloaded.
  58. /// - cacheKey: The key used to store the downloaded image in the cache. If `nil`, the `absoluteString` of `url`
  59. /// is used as the cache key.
  60. /// - Returns: A Kingfisher-compatible image view for future configuration or embedding into another `SwiftUI.View`.
  61. public static func url(
  62. _ url: URL?, cacheKey: String? = nil
  63. ) -> Self
  64. {
  65. source(url?.convertToSource(overrideCacheKey: cacheKey))
  66. }
  67. /// Creates a Kingfisher-compatible image view with a given ``ImageDataProvider``.
  68. ///
  69. /// - Parameters:
  70. /// - provider: The ``ImageDataProvider`` object that contains information about the data.
  71. /// - Returns: A Kingfisher-compatible image view for future configuration or embedding into another `SwiftUI.View`.
  72. public static func dataProvider(
  73. _ provider: ImageDataProvider?
  74. ) -> Self
  75. {
  76. source(provider?.convertToSource())
  77. }
  78. /// Creates a builder for the provided raw data and a cache key.
  79. ///
  80. /// - Parameters:
  81. /// - data: The data object from which the image should be created.
  82. /// - cacheKey: The key used to store the downloaded image in the cache.
  83. /// - Returns: A Kingfisher-compatible image view for future configuration or embedding into another `SwiftUI.View`.
  84. public static func data(
  85. _ data: Data?, cacheKey: String
  86. ) -> Self
  87. {
  88. if let data = data {
  89. return dataProvider(RawImageDataProvider(data: data, cacheKey: cacheKey))
  90. } else {
  91. return dataProvider(nil)
  92. }
  93. }
  94. }
  95. @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
  96. extension KFImageProtocol {
  97. /// Sets a placeholder `View` that is displayed during the image loading, with a progress parameter as input.
  98. ///
  99. /// - Parameter content: A view that represents the placeholder.
  100. /// - Returns: A Kingfisher-compatible image view that includes the provided `content` as its placeholder.
  101. public func placeholder<P: View>(@ViewBuilder _ content: @escaping (Progress) -> P) -> Self {
  102. context.placeholder = { progress in
  103. return AnyView(content(progress))
  104. }
  105. return self
  106. }
  107. /// Sets a placeholder `View` that is displayed during the image loading.
  108. ///
  109. /// - Parameter content: A view that represents the placeholder.
  110. /// - Returns: A Kingfisher-compatible image view that includes the provided `content` as its placeholder.
  111. public func placeholder<P: View>(@ViewBuilder _ content: @escaping () -> P) -> Self {
  112. placeholder { _ in content() }
  113. }
  114. /// Enables canceling the download task associated with `self` when the view disappears.
  115. ///
  116. /// - Parameter flag: A boolean value indicating whether to cancel the task.
  117. /// - Returns: A Kingfisher-compatible image view that cancels the download task when it 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. ///
  124. /// - Parameter duration: The duration of the fade transition.
  125. /// - Returns: A Kingfisher-compatible image view with the applied changes.
  126. ///
  127. /// Kingfisher will use the fade transition to animate the image if it is downloaded from the web. The transition
  128. /// will not occur when the image is retrieved from either memory or disk cache by default. If you need the
  129. /// transition to occur even when the image is retrieved from the cache, also call
  130. /// ``KFImageProtocol/forceRefresh(_:)`` on the returned view.
  131. public func fade(duration: TimeInterval) -> Self {
  132. context.options.transition = .fade(duration)
  133. return self
  134. }
  135. /// Sets whether to start the image loading before the view actually appears.
  136. ///
  137. /// - Parameter flag: A boolean value indicating whether the image loading should happen before the view appears. The default is `true`.
  138. /// - Returns: A Kingfisher-compatible image view with the applied changes.
  139. ///
  140. /// By default, Kingfisher performs lazy loading for `KFImage`. The image loading won't start until the view's
  141. /// `onAppear` is called. However, sometimes you may want to trigger aggressive loading for the view. By enabling
  142. /// this, the `KFImage` will attempt to load the view when its `body` is evaluated if the image loading has not
  143. /// yet started or if a previous loading attempt failed.
  144. ///
  145. /// > Important: This was a temporary workaround for an issue that arose in iOS 16, where the SwiftUI view's
  146. /// > `onAppear` was not called when it was deeply embedded inside a `List` or `ForEach`. This is no longer necessary
  147. /// > if built with Xcode 14.3 and deployed to iOS 16.4 or later. So, it is not needed anymore.
  148. /// >
  149. /// > Enabling this may cause performance regression, especially if you have a lot of images to load in the view.
  150. /// > Use it at your own risk.
  151. /// >
  152. /// > Please refer to [#1988](https://github.com/onevcat/Kingfisher/issues/1988) for more information.
  153. public func startLoadingBeforeViewAppear(_ flag: Bool = true) -> Self {
  154. context.startLoadingBeforeViewAppear = flag
  155. return self
  156. }
  157. }
  158. #endif