KFAnimatedImage.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // KFAnimatedImage.swift
  3. // Kingfisher
  4. //
  5. // Created by wangxingbin on 2021/4/29.
  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) && !os(watchOS)
  27. import SwiftUI
  28. import Combine
  29. /// Represents an animated image view in SwiftUI that manages its content using Kingfisher.
  30. ///
  31. /// Similar to ``KFImage``, this view provides support for animated image formats like GIF.
  32. ///
  33. /// - Important: Like ``KFImage``, `KFAnimatedImage` loads disk cached images synchronously by default
  34. /// (`.loadDiskFileSynchronously()` is enabled). This prevents image flickering during SwiftUI view updates
  35. /// but may impact performance when loading large animated images from disk. You can disable this behavior
  36. /// by calling `.loadDiskFileSynchronously(false)` if you prefer better loading performance over visual consistency.
  37. ///
  38. @available(iOS 14.0, macOS 11.0, tvOS 14.0, *)
  39. public struct KFAnimatedImage: KFImageProtocol {
  40. public typealias HoldingView = KFAnimatedImageViewRepresenter
  41. public var context: Context<HoldingView>
  42. public init(context: KFImage.Context<HoldingView>) {
  43. self.context = context
  44. }
  45. /// Configures current rendering view with a `block`. This block will be applied when the under-hood
  46. /// `AnimatedImageView` is created in `UIViewRepresentable.makeUIView(context:)`
  47. ///
  48. /// - Parameter block: The block applies to the animated image view.
  49. /// - Returns: A `KFAnimatedImage` view that being configured by the `block`.
  50. public func configure(_ block: @escaping (HoldingView.RenderingView) -> Void) -> Self {
  51. context.renderConfigurations.append(block)
  52. return self
  53. }
  54. }
  55. #if os(macOS)
  56. @available(macOS 11.0, *)
  57. typealias KFCrossPlatformViewRepresentable = NSViewRepresentable
  58. #else
  59. @available(iOS 14.0, tvOS 14.0, watchOS 7.0, *)
  60. typealias KFCrossPlatformViewRepresentable = UIViewRepresentable
  61. #endif
  62. /// A wrapped `UIViewRepresentable` of `AnimatedImageView`
  63. @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
  64. public struct KFAnimatedImageViewRepresenter: KFCrossPlatformViewRepresentable, KFImageHoldingView, Sendable {
  65. public typealias RenderingView = AnimatedImageView
  66. public static func created(from image: KFCrossPlatformImage?, context: KFImage.Context<Self>) -> KFAnimatedImageViewRepresenter {
  67. KFAnimatedImageViewRepresenter(image: image, context: context)
  68. }
  69. var image: KFCrossPlatformImage?
  70. let context: KFImage.Context<KFAnimatedImageViewRepresenter>
  71. #if os(macOS)
  72. public func makeNSView(context: Context) -> AnimatedImageView {
  73. return makeImageView()
  74. }
  75. public func updateNSView(_ nsView: AnimatedImageView, context: Context) {
  76. updateImageView(nsView)
  77. }
  78. #else
  79. public func makeUIView(context: Context) -> AnimatedImageView {
  80. return makeImageView()
  81. }
  82. public func updateUIView(_ uiView: AnimatedImageView, context: Context) {
  83. updateImageView(uiView)
  84. }
  85. #endif
  86. @MainActor
  87. private func makeImageView() -> AnimatedImageView {
  88. let view = AnimatedImageView()
  89. #if !os(macOS)
  90. view.isUserInteractionEnabled = true
  91. #endif
  92. self.context.renderConfigurations.forEach { $0(view) }
  93. view.image = image
  94. // Allow SwiftUI scale (fit/fill) working fine.
  95. view.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  96. view.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
  97. return view
  98. }
  99. @MainActor
  100. private func updateImageView(_ imageView: AnimatedImageView) {
  101. imageView.image = image
  102. }
  103. }
  104. #if DEBUG
  105. @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
  106. struct KFAnimatedImage_Previews: PreviewProvider {
  107. static var previews: some View {
  108. Group {
  109. KFAnimatedImage(source: .network(URL(string: "https://raw.githubusercontent.com/onevcat/Kingfisher-TestImages/master/DemoAppImage/GIF/1.gif")!))
  110. .onSuccess { r in
  111. print(r)
  112. }
  113. .placeholder {
  114. ProgressView()
  115. }
  116. .padding()
  117. }
  118. }
  119. }
  120. #endif
  121. #endif