KFImage.swift 4.5 KB

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