KFImageRenderer.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 {
  33. @StateObject var binder: KFImage.ImageBinder = .init()
  34. let context: KFImage.Context<HoldingView>
  35. var body: some View {
  36. if context.startLoadingBeforeViewAppear && !binder.loadingOrSucceeded {
  37. DispatchQueue.main.async { binder.start(context: context) }
  38. }
  39. return ZStack {
  40. context.configurations
  41. .reduce(HoldingView.created(from: binder.loadedImage, context: context)) {
  42. current, config in config(current)
  43. }
  44. .opacity(binder.loaded ? 1.0 : 0.0)
  45. if binder.loadedImage == nil {
  46. ZStack {
  47. if let placeholder = context.placeholder, let view = placeholder(binder.progress) {
  48. view
  49. } else {
  50. Color.clear
  51. }
  52. }
  53. .onAppear { [weak binder = self.binder] in
  54. guard let binder = binder else {
  55. return
  56. }
  57. if !binder.loadingOrSucceeded {
  58. binder.start(context: context)
  59. }
  60. }
  61. .onDisappear { [weak binder = self.binder] in
  62. guard let binder = binder else {
  63. return
  64. }
  65. if context.cancelOnDisappear {
  66. binder.cancel()
  67. }
  68. }
  69. }
  70. }
  71. // Workaround for https://github.com/onevcat/Kingfisher/issues/1988
  72. // on iOS 16 there seems to be a bug that when in a List, the `onAppear` of the `ZStack` above in the
  73. // `binder.loadedImage == nil` not get called. Adding this empty `onAppear` fixes it and the life cycle can
  74. // work again.
  75. //
  76. // There is another "fix": adding an `else` clause and put a `Color.clear` there. But I believe this `onAppear`
  77. // should work better.
  78. //
  79. // It should be a bug in iOS 16, I guess it is some kinds of over-optimization in list cell loading caused it.
  80. .onAppear()
  81. }
  82. }
  83. @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
  84. extension Image {
  85. // Creates an Image with either UIImage or NSImage.
  86. init(crossPlatformImage: KFCrossPlatformImage?) {
  87. #if canImport(UIKit)
  88. self.init(uiImage: crossPlatformImage ?? KFCrossPlatformImage())
  89. #elseif canImport(AppKit)
  90. self.init(nsImage: crossPlatformImage ?? KFCrossPlatformImage())
  91. #endif
  92. }
  93. }
  94. #if canImport(UIKit)
  95. @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
  96. extension UIImage.Orientation {
  97. func toSwiftUI() -> Image.Orientation {
  98. switch self {
  99. case .down: return .down
  100. case .up: return .up
  101. case .left: return .left
  102. case .right: return .right
  103. case .upMirrored: return .upMirrored
  104. case .downMirrored: return .downMirrored
  105. case .leftMirrored: return .leftMirrored
  106. case .rightMirrored: return .rightMirrored
  107. @unknown default: return .up
  108. }
  109. }
  110. }
  111. #endif
  112. #endif