KFImageOptions.swift 6.9 KB

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