KFImageRenderer.swift 3.6 KB

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