KFImage.swift 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // KFImage.swift
  3. // Kingfisher
  4. //
  5. // Created by onevcat on 2019/06/26.
  6. //
  7. // Copyright (c) 2019 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 Combine
  28. import SwiftUI
  29. @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
  30. public struct KFImage: KFImageProtocol {
  31. public typealias HoldingView = Image
  32. public internal (set) var context: Context<HoldingView>
  33. /// Creates a Kingfisher compatible image view to load image from the given `Source`.
  34. /// - Parameters:
  35. /// - source: The image `Source` defining where to load the target image.
  36. /// - isLoaded: Whether the image is loaded or not. This provides a way to inspect the internal loading
  37. /// state. `true` if the image is loaded successfully. Otherwise, `false`. Do not set the
  38. /// wrapped value from outside.
  39. public init(source: Source?, isLoaded: Binding<Bool> = .constant(false)) {
  40. let binder = ImageBinder(source: source, isLoaded: isLoaded)
  41. self.init(binder: binder)
  42. }
  43. /// Creates a Kingfisher compatible image view to load image from the given `URL`.
  44. /// - Parameters:
  45. /// - source: The image `Source` defining where to load the target image.
  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. public init(_ url: URL?, isLoaded: Binding<Bool> = .constant(false)) {
  50. self.init(source: url?.convertToSource(), isLoaded: isLoaded)
  51. }
  52. init(binder: ImageBinder) {
  53. self.context = Context<HoldingView>(binder: binder)
  54. }
  55. }
  56. @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
  57. extension KFImage {
  58. public struct Context<HoldingView: KFImageHoldingView> {
  59. var binder: ImageBinder
  60. var configurations: [(HoldingView) -> HoldingView] = []
  61. var cancelOnDisappear: Bool = false
  62. var placeholder: AnyView? = nil
  63. init(binder: ImageBinder) {
  64. self.binder = binder
  65. }
  66. }
  67. }
  68. // MARK: - Image compatibility.
  69. @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
  70. extension KFImage {
  71. /// Configures current image with a `block`. This block will be lazily applied when creating the final `Image`.
  72. /// - Parameter block: The block applies to loaded image.
  73. /// - Returns: A `KFImage` view that configures internal `Image` with `block`.
  74. public func configure(_ block: @escaping (Image) -> Image) -> KFImage {
  75. var result = self
  76. result.context.configurations.append(block)
  77. return result
  78. }
  79. public func resizable(
  80. capInsets: EdgeInsets = EdgeInsets(),
  81. resizingMode: Image.ResizingMode = .stretch) -> KFImage
  82. {
  83. configure { $0.resizable(capInsets: capInsets, resizingMode: resizingMode) }
  84. }
  85. public func renderingMode(_ renderingMode: Image.TemplateRenderingMode?) -> KFImage {
  86. configure { $0.renderingMode(renderingMode) }
  87. }
  88. public func interpolation(_ interpolation: Image.Interpolation) -> KFImage {
  89. configure { $0.interpolation(interpolation) }
  90. }
  91. public func antialiased(_ isAntialiased: Bool) -> KFImage {
  92. configure { $0.antialiased(isAntialiased) }
  93. }
  94. }
  95. // MARK: - Deprecated
  96. @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
  97. extension KFImage {
  98. /// Creates a Kingfisher compatible image view to load image from the given `Source`.
  99. /// - Parameter source: The image `Source` defining where to load the target image.
  100. /// - Parameter options: The options should be applied when loading the image.
  101. /// Some UIKit related options (such as `ImageTransition.flip`) are not supported.
  102. /// - Parameter isLoaded: Whether the image is loaded or not. This provides a way to inspect the internal loading
  103. /// state. `true` if the image is loaded successfully. Otherwise, `false`. Do not set the
  104. /// wrapped value from outside.
  105. /// - Deprecated: Some options are not available in SwiftUI yet. Use `KFImage(source:isLoaded:)` to create a
  106. /// `KFImage` and configure the options through modifier instead. See methods of `KFOptionSetter`
  107. /// for more.
  108. @available(*, deprecated, message: "Some options are not available in SwiftUI yet. Use `KFImage(source:isLoaded:)` to create a `KFImage` and configure the options through modifier instead.")
  109. public init(source: Source?, options: KingfisherOptionsInfo? = nil, isLoaded: Binding<Bool> = .constant(false)) {
  110. let binder = KFImage.ImageBinder(source: source, options: options, isLoaded: isLoaded)
  111. self.init(binder: binder)
  112. }
  113. /// Creates a Kingfisher compatible image view to load image from the given `URL`.
  114. /// - Parameter url: The image URL from where to load the target image.
  115. /// - Parameter options: The options should be applied when loading the image.
  116. /// Some UIKit related options (such as `ImageTransition.flip`) are not supported.
  117. /// - Parameter isLoaded: Whether the image is loaded or not. This provides a way to inspect the internal loading
  118. /// state. `true` if the image is loaded successfully. Otherwise, `false`. Do not set the
  119. /// wrapped value from outside.
  120. /// - Deprecated: Some options are not available in SwiftUI yet. Use `KFImage(_:isLoaded:)` to create a
  121. /// `KFImage` and configure the options through modifier instead. See methods of `KFOptionSetter`
  122. /// for more.
  123. @available(*, deprecated, message: "Some options are not available in SwiftUI yet. Use `KFImage(_:isLoaded:)` to create a `KFImage` and configure the options through modifier instead.")
  124. init(_ url: URL?, options: KingfisherOptionsInfo? = nil, isLoaded: Binding<Bool> = .constant(false)) {
  125. self.init(source: url?.convertToSource(), options: options, isLoaded: isLoaded)
  126. }
  127. }
  128. #if DEBUG
  129. @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
  130. struct KFImage_Previews : PreviewProvider {
  131. static var previews: some View {
  132. Group {
  133. KFImage(source: .network(URL(string: "https://raw.githubusercontent.com/onevcat/Kingfisher/master/images/logo.png")!))
  134. .onSuccess { r in
  135. print(r)
  136. }
  137. .resizable()
  138. .aspectRatio(contentMode: .fit)
  139. .padding()
  140. }
  141. }
  142. }
  143. #endif
  144. #endif