KFImageOptions.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. import SwiftUI
  27. // MARK: - KFImage creating.
  28. @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
  29. extension KFImage {
  30. /// Creates a `KFImage` for a given `Source`.
  31. /// - Parameters:
  32. /// - source: The `Source` object defines data information from network or a data provider.
  33. /// - isLoaded: Whether the image is loaded or not. This provides a way to inspect the internal loading
  34. /// state. `true` if the image is loaded successfully. Otherwise, `false`. Do not set the
  35. /// wrapped value from outside.
  36. /// - Returns: A `KFImage` for future configuration or embedding to a `SwiftUI.View`.
  37. public static func source(
  38. _ source: Source?, isLoaded: Binding<Bool> = .constant(false)
  39. ) -> KFImage
  40. {
  41. KFImage(source: source, isLoaded: isLoaded)
  42. }
  43. /// Creates a `KFImage` for a given `Resource`.
  44. /// - Parameters:
  45. /// - source: The `Resource` object defines data information like key or URL.
  46. /// - isLoaded: Whether the image is loaded or not. This provides a way to inspect the internal loading
  47. /// state. `true` if the image is loaded successfully. Otherwise, `false`. Do not set the
  48. /// wrapped value from outside.
  49. /// - Returns: A `KFImage` for future configuration or embedding to a `SwiftUI.View`.
  50. public static func resource(
  51. _ resource: Resource?, isLoaded: Binding<Bool> = .constant(false)
  52. ) -> KFImage
  53. {
  54. .source(resource?.convertToSource(), isLoaded: isLoaded)
  55. }
  56. /// Creates a `KFImage` for a given `URL`.
  57. /// - Parameters:
  58. /// - url: The URL where the image should be downloaded.
  59. /// - cacheKey: The key used to store the downloaded image in cache.
  60. /// If `nil`, the `absoluteString` of `url` is used as the cache key.
  61. /// - isLoaded: Whether the image is loaded or not. This provides a way to inspect the internal loading
  62. /// state. `true` if the image is loaded successfully. Otherwise, `false`. Do not set the
  63. /// wrapped value from outside.
  64. /// - Returns: A `KFImage` for future configuration or embedding to a `SwiftUI.View`.
  65. public static func url(
  66. _ url: URL?, cacheKey: String? = nil, isLoaded: Binding<Bool> = .constant(false)
  67. ) -> KFImage
  68. {
  69. source(url?.convertToSource(), isLoaded: isLoaded)
  70. }
  71. /// Creates a `KFImage` for a given `ImageDataProvider`.
  72. /// - Parameters:
  73. /// - provider: The `ImageDataProvider` object contains information about the data.
  74. /// - isLoaded: Whether the image is loaded or not. This provides a way to inspect the internal loading
  75. /// state. `true` if the image is loaded successfully. Otherwise, `false`. Do not set the
  76. /// wrapped value from outside.
  77. /// - Returns: A `KFImage` for future configuration or embedding to a `SwiftUI.View`.
  78. public static func dataProvider(
  79. _ provider: ImageDataProvider, isLoaded: Binding<Bool> = .constant(false)
  80. ) -> KFImage
  81. {
  82. source(.provider(provider), isLoaded: isLoaded)
  83. }
  84. /// Creates a builder for some given raw data and a cache key.
  85. /// - Parameters:
  86. /// - data: The data object from which the image should be created.
  87. /// - cacheKey: The key used to store the downloaded image in cache.
  88. /// - isLoaded: Whether the image is loaded or not. This provides a way to inspect the internal loading
  89. /// state. `true` if the image is loaded successfully. Otherwise, `false`. Do not set the
  90. /// wrapped value from outside.
  91. /// - Returns: A `KFImage` for future configuration or embedding to a `SwiftUI.View`.
  92. public static func data(
  93. _ data: Data, cacheKey: String, isLoaded: Binding<Bool> = .constant(false)
  94. ) -> KFImage
  95. {
  96. source(.provider(RawImageDataProvider(data: data, cacheKey: cacheKey)), isLoaded: isLoaded)
  97. }
  98. }
  99. @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
  100. extension KFImage {
  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<Content: View>(@ViewBuilder _ content: () -> Content) -> KFImage {
  105. let v = content()
  106. var result = self
  107. result.placeholder = AnyView(v)
  108. return result
  109. }
  110. /// Sets cancelling the download task bound to `self` when the view disappearing.
  111. /// - Parameter flag: Whether cancel the task or not.
  112. /// - Returns: A `KFImage` view that cancels downloading task when disappears.
  113. public func cancelOnDisappear(_ flag: Bool) -> KFImage {
  114. var result = self
  115. result.cancelOnDisappear = flag
  116. return result
  117. }
  118. }