KFImageRenderer.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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)
  27. import SwiftUI
  28. /// A Kingfisher compatible SwiftUI `View` to load an image from a `Source`.
  29. /// Declaring a `KFImage` in a `View`'s body to trigger loading from the given `Source`.
  30. @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
  31. struct KFImageRenderer<HoldingView> : View where HoldingView: KFImageHoldingView {
  32. /// An image binder that manages loading and cancelling image related task.
  33. @StateObject var binder: KFImage.ImageBinder
  34. // Acts as a placeholder when loading an image.
  35. var placeholder: AnyView?
  36. // Whether the download task should be cancelled when the view disappears.
  37. let cancelOnDisappear: Bool
  38. // Configurations should be performed on the image.
  39. let configurations: [(HoldingView) -> HoldingView]
  40. /// Declares the content and behavior of this view.
  41. @ViewBuilder
  42. var body: some View {
  43. if let image = binder.loadedImage {
  44. configurations
  45. .reduce(HoldingView.created(from: image)) {
  46. current, config in config(current)
  47. }
  48. .opacity(binder.loaded ? 1.0 : 0.0)
  49. } else {
  50. Group {
  51. if placeholder != nil {
  52. placeholder
  53. } else {
  54. Color.clear
  55. }
  56. }
  57. .onAppear { [weak binder = self.binder] in
  58. guard let binder = binder else {
  59. return
  60. }
  61. if !binder.loadingOrSucceeded {
  62. binder.start()
  63. }
  64. }
  65. .onDisappear { [weak binder = self.binder] in
  66. guard let binder = binder else {
  67. return
  68. }
  69. if self.cancelOnDisappear {
  70. binder.cancel()
  71. }
  72. }
  73. }
  74. }
  75. }
  76. @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
  77. extension Image {
  78. // Creates an Image with either UIImage or NSImage.
  79. init(crossPlatformImage: KFCrossPlatformImage) {
  80. #if canImport(UIKit)
  81. self.init(uiImage: crossPlatformImage)
  82. #elseif canImport(AppKit)
  83. self.init(nsImage: crossPlatformImage)
  84. #endif
  85. }
  86. }
  87. #if canImport(UIKit)
  88. @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
  89. extension UIImage.Orientation {
  90. func toSwiftUI() -> Image.Orientation {
  91. switch self {
  92. case .down: return .down
  93. case .up: return .up
  94. case .left: return .left
  95. case .right: return .right
  96. case .upMirrored: return .upMirrored
  97. case .downMirrored: return .downMirrored
  98. case .leftMirrored: return .leftMirrored
  99. case .rightMirrored: return .rightMirrored
  100. @unknown default: return .up
  101. }
  102. }
  103. }
  104. #endif
  105. #endif