KFImageRenderer.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // KFImageRenderer.swift
  3. // Kingfisher
  4. //
  5. // Created by onevcat on 2021/05/08.
  6. //
  7. // Copyright (c) 2021 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 SwiftUI
  28. import Combine
  29. /// A Kingfisher compatible SwiftUI `View` to load an image from a `Source`.
  30. /// Declaring a `KFImage` in a `View`'s body to trigger loading from the given `Source`.
  31. @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
  32. struct KFImageRenderer<HoldingView> : View where HoldingView: KFImageHoldingView & Sendable {
  33. @StateObject var binder: KFImage.ImageBinder = .init()
  34. let context: KFImage.Context<HoldingView>
  35. var body: some View {
  36. if context.startLoadingBeforeViewAppear && !binder.loadingOrSucceeded && !binder.animating {
  37. binder.markLoading()
  38. DispatchQueue.main.async { binder.start(context: context) }
  39. }
  40. return ZStack {
  41. if context.swiftUITransition != nil {
  42. // SwiftUI loadTransition: insert/remove view for proper transition behavior
  43. if binder.loaded {
  44. renderedImage()
  45. }
  46. } else {
  47. // Fade transition or no transition: use opacity control
  48. renderedImage()
  49. .opacity(binder.loaded ? 1.0 : 0.0)
  50. }
  51. if binder.loadedImage == nil {
  52. ZStack {
  53. // Priority: failureView > placeholder > Color.clear
  54. // failureView is only set when image loading fails
  55. if let failureView = binder.failureView {
  56. failureView()
  57. } else if let placeholder = context.placeholder {
  58. placeholder(binder.progress)
  59. } else {
  60. Color.clear
  61. }
  62. }
  63. .onAppear { [weak binder = self.binder] in
  64. guard let binder = binder else {
  65. return
  66. }
  67. if !binder.loadingOrSucceeded {
  68. binder.start(context: context)
  69. } else {
  70. if context.reducePriorityOnDisappear {
  71. binder.restorePriorityOnAppear()
  72. }
  73. }
  74. }
  75. .onDisappear { [weak binder = self.binder] in
  76. guard let binder = binder else {
  77. return
  78. }
  79. if context.cancelOnDisappear {
  80. binder.cancel()
  81. } else if context.reducePriorityOnDisappear {
  82. binder.reducePriorityOnDisappear()
  83. }
  84. }
  85. }
  86. }
  87. // Workaround for https://github.com/onevcat/Kingfisher/issues/1988
  88. // on iOS 16 there seems to be a bug that when in a List, the `onAppear` of the `ZStack` above in the
  89. // `binder.loadedImage == nil` not get called. Adding this empty `onAppear` fixes it and the life cycle can
  90. // work again.
  91. //
  92. // There is another "fix": adding an `else` clause and put a `Color.clear` there. But I believe this `onAppear`
  93. // should work better.
  94. //
  95. // It should be a bug in iOS 16, I guess it is some kinds of over-optimization in list cell loading caused it.
  96. .onAppear()
  97. }
  98. @ViewBuilder
  99. private func renderedImage() -> some View {
  100. if let swiftUITransition = context.swiftUITransition {
  101. // Apply SwiftUI loadTransition as the last step for correct rendering order
  102. configuredImage.transition(swiftUITransition)
  103. } else {
  104. configuredImage
  105. }
  106. }
  107. @ViewBuilder
  108. private var configuredImage: some View {
  109. let configuredImage = context.configurations
  110. .reduce(HoldingView.created(from: binder.loadedImage, context: context)) {
  111. current, config in config(current)
  112. }
  113. // Apply contentConfiguration first, then loadTransition as the final step
  114. if let contentConfiguration = context.contentConfiguration {
  115. contentConfiguration(configuredImage)
  116. } else {
  117. configuredImage
  118. }
  119. }
  120. }
  121. @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
  122. extension Image {
  123. // Creates an Image with either UIImage or NSImage.
  124. init(crossPlatformImage: KFCrossPlatformImage?) {
  125. #if canImport(UIKit)
  126. self.init(uiImage: crossPlatformImage ?? KFCrossPlatformImage())
  127. #elseif canImport(AppKit)
  128. self.init(nsImage: crossPlatformImage ?? KFCrossPlatformImage())
  129. #endif
  130. }
  131. }
  132. #if canImport(UIKit)
  133. @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
  134. extension UIImage.Orientation {
  135. func toSwiftUI() -> Image.Orientation {
  136. switch self {
  137. case .down: return .down
  138. case .up: return .up
  139. case .left: return .left
  140. case .right: return .right
  141. case .upMirrored: return .upMirrored
  142. case .downMirrored: return .downMirrored
  143. case .leftMirrored: return .leftMirrored
  144. case .rightMirrored: return .rightMirrored
  145. @unknown default: return .up
  146. }
  147. }
  148. }
  149. #endif
  150. #endif