KFImage.swift 5.1 KB

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