KFImageRenderer.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. ZStack {
  37. context.configurations
  38. .reduce(HoldingView.created(from: binder.loadedImage, context: context)) {
  39. current, config in config(current)
  40. }
  41. .opacity(binder.loaded ? 1.0 : 0.0)
  42. if binder.loadedImage == nil {
  43. Group {
  44. if let placeholder = context.placeholder, let view = placeholder(binder.progress) {
  45. view
  46. } else {
  47. Color.clear
  48. }
  49. }
  50. .onAppear { [weak binder = self.binder] in
  51. guard let binder = binder else {
  52. return
  53. }
  54. if !binder.loadingOrSucceeded {
  55. binder.start(context: context)
  56. }
  57. }
  58. .onDisappear { [weak binder = self.binder] in
  59. guard let binder = binder else {
  60. return
  61. }
  62. if context.cancelOnDisappear {
  63. binder.cancel()
  64. }
  65. }
  66. }
  67. }
  68. }
  69. }
  70. @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
  71. extension Image {
  72. // Creates an Image with either UIImage or NSImage.
  73. init(crossPlatformImage: KFCrossPlatformImage?) {
  74. #if canImport(UIKit)
  75. self.init(uiImage: crossPlatformImage ?? KFCrossPlatformImage())
  76. #elseif canImport(AppKit)
  77. self.init(nsImage: crossPlatformImage ?? KFCrossPlatformImage())
  78. #endif
  79. }
  80. }
  81. #if canImport(UIKit)
  82. @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
  83. extension UIImage.Orientation {
  84. func toSwiftUI() -> Image.Orientation {
  85. switch self {
  86. case .down: return .down
  87. case .up: return .up
  88. case .left: return .left
  89. case .right: return .right
  90. case .upMirrored: return .upMirrored
  91. case .downMirrored: return .downMirrored
  92. case .leftMirrored: return .leftMirrored
  93. case .rightMirrored: return .rightMirrored
  94. @unknown default: return .up
  95. }
  96. }
  97. }
  98. #endif
  99. #endif