KFImageRenderer.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. import SwiftUI
  27. /// A Kingfisher compatible SwiftUI `View` to load an image from a `Source`.
  28. /// Declaring a `KFImage` in a `View`'s body to trigger loading from the given `Source`.
  29. @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
  30. struct KFImageRenderer<HoldingView> : View where HoldingView: KFImageHoldingView {
  31. /// An image binder that manages loading and cancelling image related task.
  32. @ObservedObject var binder: KFImage.ImageBinder
  33. // Acts as a placeholder when loading an image.
  34. var placeholder: AnyView?
  35. // Whether the download task should be cancelled when the view disappears.
  36. let cancelOnDisappear: Bool
  37. // Configurations should be performed on the image.
  38. let configurations: [(HoldingView) -> HoldingView]
  39. init(_ context: KFImage.Context<HoldingView>) {
  40. self.binder = context.binder
  41. self.configurations = context.configurations
  42. self.placeholder = context.placeholder
  43. self.cancelOnDisappear = context.cancelOnDisappear
  44. }
  45. /// Declares the content and behavior of this view.
  46. @ViewBuilder
  47. var body: some View {
  48. if let image = binder.loadedImage {
  49. configurations
  50. .reduce(HoldingView.created(from: image)) {
  51. current, config in config(current)
  52. }
  53. .opacity(binder.loaded ? 1.0 : 0.0)
  54. } else {
  55. Group {
  56. if placeholder != nil {
  57. placeholder
  58. } else {
  59. Color.clear
  60. }
  61. }
  62. .onAppear { [weak binder = self.binder] in
  63. guard let binder = binder else {
  64. return
  65. }
  66. if !binder.loadingOrSucceeded {
  67. binder.start()
  68. }
  69. }
  70. .onDisappear { [weak binder = self.binder] in
  71. guard let binder = binder else {
  72. return
  73. }
  74. if self.cancelOnDisappear {
  75. binder.cancel()
  76. }
  77. }
  78. }
  79. }
  80. }
  81. @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
  82. extension Image: KFImageHoldingView {
  83. public static func created(from image: KFCrossPlatformImage) -> Image {
  84. if #available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
  85. return Image(crossPlatformImage: image)
  86. } else {
  87. #if canImport(UIKit)
  88. // The CG image is used to solve #1395
  89. // It should be not necessary if SwiftUI.Image can handle resizing correctly when created
  90. // by `Image.init(uiImage:)`. (The orientation information should be already contained in
  91. // a `UIImage`)
  92. // https://github.com/onevcat/Kingfisher/issues/1395
  93. //
  94. // This issue happens on iOS 13 and was fixed by Apple from iOS 14.
  95. if let cgImage = image.cgImage {
  96. return Image(decorative: cgImage, scale: image.scale, orientation: image.imageOrientation.toSwiftUI())
  97. } else {
  98. return Image(crossPlatformImage: image)
  99. }
  100. #else
  101. return Image(crossPlatformImage: image)
  102. #endif
  103. }
  104. }
  105. }
  106. @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
  107. extension Image {
  108. // Creates an Image with either UIImage or NSImage.
  109. init(crossPlatformImage: KFCrossPlatformImage) {
  110. #if canImport(UIKit)
  111. self.init(uiImage: crossPlatformImage)
  112. #elseif canImport(AppKit)
  113. self.init(nsImage: crossPlatformImage)
  114. #endif
  115. }
  116. }
  117. #if canImport(UIKit)
  118. @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
  119. extension UIImage.Orientation {
  120. func toSwiftUI() -> Image.Orientation {
  121. switch self {
  122. case .down: return .down
  123. case .up: return .up
  124. case .left: return .left
  125. case .right: return .right
  126. case .upMirrored: return .upMirrored
  127. case .downMirrored: return .downMirrored
  128. case .leftMirrored: return .leftMirrored
  129. case .rightMirrored: return .rightMirrored
  130. @unknown default: return .up
  131. }
  132. }
  133. }
  134. #endif