KFImage.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. init(crossPlatformImage: KFCrossPlatformImage) {
  31. #if canImport(UIKit)
  32. self.init(uiImage: crossPlatformImage)
  33. #elseif canImport(AppKit)
  34. self.init(nsImage: crossPlatformImage)
  35. #endif
  36. }
  37. }
  38. @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
  39. public struct KFImage: View {
  40. var placeholder = Image(crossPlatformImage: .init())
  41. var configs: [(Image) -> Image]
  42. @ObjectBinding public private(set) var binder: ImageBinder
  43. private let onFailureDelegate = Delegate<KingfisherError, Void>()
  44. private let onSuccessDelegate = Delegate<RetrieveImageResult, Void>()
  45. public init(url: URL) {
  46. binder = ImageBinder(url: url)
  47. configs = []
  48. }
  49. public var body: some View {
  50. print("Loading body \(binder.url)")
  51. let image = binder.image.map { Image(crossPlatformImage: $0) } ?? placeholder
  52. return configs
  53. .reduce(image) { current, config in config(current) }
  54. .onAppear { [unowned binder] in
  55. binder.subscriber?.cancel()
  56. binder.subscriber = binder.subject.sink(
  57. receiveCompletion: { complete in
  58. switch complete {
  59. case .failure(let error):
  60. self.onFailureDelegate.call(error)
  61. case .finished: break
  62. }
  63. binder.subscriber?.cancel()
  64. binder.subscriber = nil
  65. },
  66. receiveValue: { result in
  67. self.onSuccessDelegate.call(result)
  68. }
  69. )
  70. print("Start!! \(binder.url)")
  71. binder.start()
  72. }
  73. }
  74. }
  75. @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
  76. extension KFImage {
  77. public func config(_ block: @escaping (Image) -> Image) -> KFImage {
  78. var result = self
  79. result.configs.append(block)
  80. return result
  81. }
  82. public func resizable(
  83. capInsets: EdgeInsets = EdgeInsets(),
  84. resizingMode: Image.ResizingMode = .stretch) -> KFImage
  85. {
  86. config { $0.resizable(capInsets: capInsets, resizingMode: resizingMode) }
  87. }
  88. public func renderingMode(_ renderingMode: Image.TemplateRenderingMode?) -> KFImage {
  89. config { $0.renderingMode(renderingMode) }
  90. }
  91. public func interpolation(_ interpolation: Image.Interpolation) -> KFImage {
  92. config { $0.interpolation(interpolation) }
  93. }
  94. public func antialiased(_ isAntialiased: Bool) -> KFImage {
  95. config { $0.antialiased(isAntialiased) }
  96. }
  97. public func placeholder(image: Image?) -> KFImage {
  98. var result = self
  99. result.placeholder = image ?? Image(crossPlatformImage: .init())
  100. return result
  101. }
  102. public func placeholder(name: String, bundle: Bundle? = nil) -> KFImage {
  103. return placeholder(image: .init(name, bundle: bundle))
  104. }
  105. public func placeholder(systemName: String) -> KFImage {
  106. return placeholder(image: .init(systemName: systemName))
  107. }
  108. }
  109. @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
  110. extension KFImage {
  111. public func onFailure(perform action: ((Error) -> Void)?) -> KFImage {
  112. onFailureDelegate.delegate(on: binder) { _, error in
  113. action?(error)
  114. }
  115. return self
  116. }
  117. public func onSuccess(perform action: ((RetrieveImageResult) -> Void)?) -> KFImage {
  118. onSuccessDelegate.delegate(on: binder) { _, result in
  119. action?(result)
  120. }
  121. return self
  122. }
  123. }
  124. #if DEBUG
  125. @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
  126. struct KFImage_Previews : PreviewProvider {
  127. static var previews: some View {
  128. KFImage(url:URL(string: "https://raw.githubusercontent.com/onevcat/Kingfisher/master/images/logo.png")!)
  129. .onSuccess { r in
  130. print(r)
  131. }
  132. .resizable()
  133. .aspectRatio(contentMode: .fit)
  134. .padding()
  135. }
  136. }
  137. #endif