KFImageOptions.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. /// - Returns: A `KFImage` for future configuration or embedding to a `SwiftUI.View`.
  36. public static func source(
  37. _ source: Source?
  38. ) -> Self
  39. {
  40. Self.init(source: source)
  41. }
  42. /// Creates a `KFImage` for a given `Resource`.
  43. /// - Parameters:
  44. /// - source: The `Resource` object defines data information like key or URL.
  45. /// - Returns: A `KFImage` for future configuration or embedding to a `SwiftUI.View`.
  46. public static func resource(
  47. _ resource: Resource?
  48. ) -> Self
  49. {
  50. source(resource?.convertToSource())
  51. }
  52. /// Creates a `KFImage` for a given `URL`.
  53. /// - Parameters:
  54. /// - url: The URL where the image should be downloaded.
  55. /// - cacheKey: The key used to store the downloaded image in cache.
  56. /// If `nil`, the `absoluteString` of `url` is used as the cache key.
  57. /// - Returns: A `KFImage` for future configuration or embedding to a `SwiftUI.View`.
  58. public static func url(
  59. _ url: URL?, cacheKey: String? = nil
  60. ) -> Self
  61. {
  62. source(url?.convertToSource(overrideCacheKey: cacheKey))
  63. }
  64. /// Creates a `KFImage` for a given `ImageDataProvider`.
  65. /// - Parameters:
  66. /// - provider: The `ImageDataProvider` object contains information about the data.
  67. /// - Returns: A `KFImage` for future configuration or embedding to a `SwiftUI.View`.
  68. public static func dataProvider(
  69. _ provider: ImageDataProvider?
  70. ) -> Self
  71. {
  72. source(provider?.convertToSource())
  73. }
  74. /// Creates a builder for some given raw data and a cache key.
  75. /// - Parameters:
  76. /// - data: The data object from which the image should be created.
  77. /// - cacheKey: The key used to store the downloaded image in cache.
  78. /// - Returns: A `KFImage` for future configuration or embedding to a `SwiftUI.View`.
  79. public static func data(
  80. _ data: Data?, cacheKey: String
  81. ) -> Self
  82. {
  83. if let data = data {
  84. return dataProvider(RawImageDataProvider(data: data, cacheKey: cacheKey))
  85. } else {
  86. return dataProvider(nil)
  87. }
  88. }
  89. }
  90. @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
  91. extension KFImageProtocol {
  92. /// Sets a placeholder `View` which shows when loading the image, with a progress parameter as input.
  93. /// - Parameter content: A view that describes the placeholder.
  94. /// - Returns: A `KFImage` view that contains `content` as its placeholder.
  95. public func placeholder<P: View>(@ViewBuilder _ content: @escaping (Progress) -> P) -> Self {
  96. context.placeholder = { progress in
  97. return AnyView(content(progress))
  98. }
  99. return self
  100. }
  101. /// Sets a placeholder `View` which shows when loading the image.
  102. /// - Parameter content: A view that describes the placeholder.
  103. /// - Returns: A `KFImage` view that contains `content` as its placeholder.
  104. public func placeholder<P: View>(@ViewBuilder _ content: @escaping () -> P) -> Self {
  105. placeholder { _ in content() }
  106. }
  107. /// Sets cancelling the download task bound to `self` when the view disappearing.
  108. /// - Parameter flag: Whether cancel the task or not.
  109. /// - Returns: A `KFImage` view that cancels downloading task when disappears.
  110. public func cancelOnDisappear(_ flag: Bool) -> Self {
  111. context.cancelOnDisappear = flag
  112. return self
  113. }
  114. /// Sets a fade transition for the image task.
  115. /// - Parameter duration: The duration of the fade transition.
  116. /// - Returns: A `KFImage` with changes applied.
  117. ///
  118. /// Kingfisher will use the fade transition to animate the image in if it is downloaded from web.
  119. /// The transition will not happen when the
  120. /// image is retrieved from either memory or disk cache by default. If you need to do the transition even when
  121. /// the image being retrieved from cache, also call `forceRefresh()` on the returned `KFImage`.
  122. public func fade(duration: TimeInterval) -> Self {
  123. context.options.transition = .fade(duration)
  124. return self
  125. }
  126. /// Sets whether to start the image loading before the view actually appears.
  127. ///
  128. /// By default, Kingfisher performs a lazy loading for `KFImage`. The image loading won't start until the view's
  129. /// `onAppear` is called. However, sometimes you may want to trigger an aggressive loading for the view. By enabling
  130. /// this, the `KFImage` will try to load the view when its `body` is evaluated when the image loading is not yet
  131. /// started or a previous loading did fail.
  132. ///
  133. /// - Parameter flag: Whether the image loading should happen before view appear. Default is `true`.
  134. /// - Returns: A `KFImage` with changes applied.
  135. ///
  136. /// - Note: This is a temporary workaround for an issue from iOS 16, where the SwiftUI view's `onAppear` is not
  137. /// called when it is deeply embedded inside a `List` or `ForEach`.
  138. /// See [#1988](https://github.com/onevcat/Kingfisher/issues/1988). It may cause performance regression, especially
  139. /// if you have a lot of images to load in the view. Use it as your own risk.
  140. ///
  141. public func startLoadingBeforeViewAppear(_ flag: Bool = true) -> Self {
  142. context.startLoadingBeforeViewAppear = flag
  143. return self
  144. }
  145. }
  146. #endif