KFAnimatedImage.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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) && canImport(UIKit) && !os(watchOS)
  27. import SwiftUI
  28. import Combine
  29. @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
  30. public struct KFAnimatedImage: KFImageProtocol {
  31. public typealias HoldingView = KFAnimatedImageViewRepresenter
  32. public var context: Context<HoldingView>
  33. public init(context: KFImage.Context<HoldingView>) {
  34. self.context = context
  35. }
  36. /// Configures current rendering view with a `block`. This block will be applied when the under-hood
  37. /// `AnimatedImageView` is created in `UIViewRepresentable.makeUIView(context:)`
  38. ///
  39. /// - Parameter block: The block applies to the animated image view.
  40. /// - Returns: A `KFAnimatedImage` view that being configured by the `block`.
  41. public func configure(_ block: @escaping (HoldingView.RenderingView) -> Void) -> Self {
  42. context.renderConfigurations.append(block)
  43. return self
  44. }
  45. }
  46. /// A wrapped `UIViewRepresentable` of `AnimatedImageView`
  47. @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
  48. public struct KFAnimatedImageViewRepresenter: UIViewRepresentable, KFImageHoldingView {
  49. public typealias RenderingView = AnimatedImageView
  50. public static func created(from image: KFCrossPlatformImage?, context: KFImage.Context<Self>) -> KFAnimatedImageViewRepresenter {
  51. KFAnimatedImageViewRepresenter(image: image, context: context)
  52. }
  53. var image: KFCrossPlatformImage?
  54. let context: KFImage.Context<KFAnimatedImageViewRepresenter>
  55. public func makeUIView(context: Context) -> AnimatedImageView {
  56. let view = AnimatedImageView()
  57. self.context.renderConfigurations.forEach { $0(view) }
  58. view.image = image
  59. // Allow SwiftUI scale (fit/fill) working fine.
  60. view.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  61. view.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
  62. return view
  63. }
  64. public func updateUIView(_ uiView: AnimatedImageView, context: Context) {
  65. uiView.image = image
  66. }
  67. }
  68. #if DEBUG
  69. @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
  70. struct KFAnimatedImage_Previews: PreviewProvider {
  71. static var previews: some View {
  72. Group {
  73. KFAnimatedImage(source: .network(URL(string: "https://raw.githubusercontent.com/onevcat/Kingfisher-TestImages/master/DemoAppImage/GIF/1.gif")!))
  74. .onSuccess { r in
  75. print(r)
  76. }
  77. .placeholder {
  78. ProgressView()
  79. }
  80. .padding()
  81. }
  82. }
  83. }
  84. #endif
  85. #endif