KFImage.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. import SwiftUI
  27. import Combine
  28. @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
  29. extension Image {
  30. // Creates an SwiftUI.Image with either UIImage or NSImage.
  31. init(crossPlatformImage: KFCrossPlatformImage) {
  32. #if canImport(UIKit)
  33. self.init(uiImage: crossPlatformImage)
  34. #elseif canImport(AppKit)
  35. self.init(nsImage: crossPlatformImage)
  36. #endif
  37. }
  38. }
  39. @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
  40. extension View {
  41. func eraseToAnyView() -> AnyView { .init(self) }
  42. }
  43. @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
  44. public struct KFImage: View {
  45. @ObjectBinding public private(set) var binder: ImageBinder
  46. var placeholder: AnyView?
  47. var cancelOnDisappear: Bool = false
  48. var configs: [(Image) -> Image]
  49. public init(_ source: Source, options: KingfisherOptionsInfo? = nil) {
  50. binder = ImageBinder(source: source, options: options)
  51. configs = []
  52. }
  53. public init(_ url: URL, options: KingfisherOptionsInfo? = nil) {
  54. self.init(.network(url), options: options)
  55. }
  56. public var body: some View {
  57. if let image = binder.image {
  58. return configs.reduce(Image(crossPlatformImage: image)) {
  59. current, config in config(current)
  60. }.eraseToAnyView()
  61. } else {
  62. let result = (placeholder ?? Image(crossPlatformImage: .init()).eraseToAnyView())
  63. .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
  64. let onAppear = result.onAppear { [unowned binder] in
  65. binder.start()
  66. }
  67. if cancelOnDisappear {
  68. return onAppear.onDisappear { [unowned binder] in
  69. binder.cancel()
  70. }.eraseToAnyView()
  71. } else {
  72. return onAppear.eraseToAnyView()
  73. }
  74. }
  75. }
  76. }
  77. @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
  78. extension KFImage {
  79. public func config(_ block: @escaping (Image) -> Image) -> KFImage {
  80. var result = self
  81. result.configs.append(block)
  82. return result
  83. }
  84. public func resizable(
  85. capInsets: EdgeInsets = EdgeInsets(),
  86. resizingMode: Image.ResizingMode = .stretch) -> KFImage
  87. {
  88. config { $0.resizable(capInsets: capInsets, resizingMode: resizingMode) }
  89. }
  90. public func renderingMode(_ renderingMode: Image.TemplateRenderingMode?) -> KFImage {
  91. config { $0.renderingMode(renderingMode) }
  92. }
  93. public func interpolation(_ interpolation: Image.Interpolation) -> KFImage {
  94. config { $0.interpolation(interpolation) }
  95. }
  96. public func antialiased(_ isAntialiased: Bool) -> KFImage {
  97. config { $0.antialiased(isAntialiased) }
  98. }
  99. public func placeholder<Content: View>(@ViewBuilder _ content: () -> Content) -> KFImage {
  100. let v = content()
  101. var result = self
  102. result.placeholder = AnyView(v)
  103. return result
  104. }
  105. public func cancelOnDisappear(_ flag: Bool) -> KFImage {
  106. var result = self
  107. result.cancelOnDisappear = flag
  108. return result
  109. }
  110. }
  111. @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
  112. extension KFImage {
  113. public func onFailure(perform action: ((Error) -> Void)?) -> KFImage {
  114. binder.setOnFailure(perform: action)
  115. return self
  116. }
  117. public func onSuccess(perform action: ((RetrieveImageResult) -> Void)?) -> KFImage {
  118. binder.setOnSuccess(perform: action)
  119. return self
  120. }
  121. public func onProgress(perform action: ((Int64, Int64) -> Void)?) -> KFImage {
  122. binder.setOnProgress(perform: action)
  123. return self
  124. }
  125. }
  126. #if DEBUG
  127. @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
  128. struct KFImage_Previews : PreviewProvider {
  129. static var previews: some View {
  130. Group {
  131. KFImage(URL(string: "https://raw.githubusercontent.com/onevcat/Kingfisher/master/images/logo.png")!)
  132. .onSuccess { r in
  133. print(r)
  134. }
  135. .resizable()
  136. .aspectRatio(contentMode: .fit)
  137. .padding()
  138. }
  139. }
  140. }
  141. #endif