KFAnimatedImage.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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)
  27. import SwiftUI
  28. @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
  29. public struct KFAnimatedImage: KFImageProtocol {
  30. public typealias Context = KFImage.Context
  31. typealias ImageBinder = KFImage.ImageBinder
  32. public typealias HoldingView = KFAnimatedImageViewRepresenter
  33. public internal (set) var context: Context<HoldingView>
  34. /// Creates a Kingfisher compatible image view to load image from the given `Source`.
  35. /// - Parameters:
  36. /// - source: The image `Source` defining where to load the target image.
  37. /// - isLoaded: Whether the image is loaded or not. This provides a way to inspect the internal loading
  38. /// state. `true` if the image is loaded successfully. Otherwise, `false`. Do not set the
  39. /// wrapped value from outside.
  40. public init(source: Source?, isLoaded: Binding<Bool> = .constant(false)) {
  41. let binder = ImageBinder(source: source, isLoaded: isLoaded)
  42. self.init(binder: binder)
  43. }
  44. /// Creates a Kingfisher compatible image view to load image from the given `URL`.
  45. /// - Parameters:
  46. /// - source: The image `Source` defining where to load the target image.
  47. /// - isLoaded: Whether the image is loaded or not. This provides a way to inspect the internal loading
  48. /// state. `true` if the image is loaded successfully. Otherwise, `false`. Do not set the
  49. /// wrapped value from outside.
  50. public init(_ url: URL?, isLoaded: Binding<Bool> = .constant(false)) {
  51. self.init(source: url?.convertToSource(), isLoaded: isLoaded)
  52. }
  53. init(binder: ImageBinder) {
  54. self.context = Context(binder: binder)
  55. }
  56. public var body: some View {
  57. KFImageRenderer<KFAnimatedImageViewRepresenter>(context)
  58. .id(context.binder)
  59. }
  60. }
  61. /// A Kingfisher compatible SwiftUI `View` to load an image from a `Source`.
  62. /// Declaring a `KFAnimatedImage` in a `View`'s body to trigger loading from the given `Source`.
  63. @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
  64. struct KFAnimatedImageRender: View {
  65. /// An image binder that manages loading and cancelling image related task.
  66. @ObservedObject var binder: KFAnimatedImage.ImageBinder
  67. // Acts as a placeholder when loading an image.
  68. var placeholder: AnyView?
  69. // Whether the download task should be cancelled when the view disappears.
  70. let cancelOnDisappear: Bool
  71. init(_ context: KFAnimatedImage.Context<Image>) {
  72. self.binder = context.binder
  73. self.placeholder = context.placeholder
  74. self.cancelOnDisappear = context.cancelOnDisappear
  75. }
  76. /// Declares the content and behavior of this view.
  77. @ViewBuilder
  78. var body: some View {
  79. if let image = binder.loadedImage {
  80. KFAnimatedImageViewRepresenter(image: image)
  81. .opacity(binder.loaded ? 1.0 : 0.0)
  82. } else {
  83. Group {
  84. if placeholder != nil {
  85. placeholder
  86. } else {
  87. Color.clear
  88. }
  89. }
  90. .onAppear { [weak binder = self.binder] in
  91. guard let binder = binder else {
  92. return
  93. }
  94. if !binder.loadingOrSucceeded {
  95. binder.start()
  96. }
  97. }
  98. .onDisappear { [weak binder = self.binder] in
  99. guard let binder = binder else {
  100. return
  101. }
  102. if self.cancelOnDisappear {
  103. binder.cancel()
  104. }
  105. }
  106. }
  107. }
  108. }
  109. /// A wrapped `UIViewRepresentable` of `AnimatedImageView`
  110. @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
  111. public struct KFAnimatedImageViewRepresenter: UIViewRepresentable, KFImageHoldingView {
  112. public static func created(from image: KFCrossPlatformImage) -> KFAnimatedImageViewRepresenter {
  113. KFAnimatedImageViewRepresenter(image: image)
  114. }
  115. var image: KFCrossPlatformImage?
  116. public func makeUIView(context: Context) -> AnimatedImageView {
  117. let view = AnimatedImageView()
  118. view.image = image
  119. return view
  120. }
  121. public func updateUIView(_ uiView: AnimatedImageView, context: Context) {
  122. uiView.image = image
  123. }
  124. }
  125. #if DEBUG
  126. @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
  127. struct KFAnimatedImage_Previews : PreviewProvider {
  128. static var previews: some View {
  129. Group {
  130. KFAnimatedImage(source: .network(URL(string: "https://raw.githubusercontent.com/onevcat/Kingfisher-TestImages/master/DemoAppImage/GIF/1.gif")!))
  131. .onSuccess { r in
  132. print(r)
  133. }
  134. .padding()
  135. }
  136. }
  137. }
  138. #endif
  139. #endif