From baeaa557c0e0122e0a4d262904a9bf4d8504bacb Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Wed, 7 Aug 2019 19:45:04 +0800 Subject: [PATCH] Update the AnimatedImage with different initializer --- SDWebImageSwiftUI/Classes/AnimatedImage.swift | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/SDWebImageSwiftUI/Classes/AnimatedImage.swift b/SDWebImageSwiftUI/Classes/AnimatedImage.swift index 1e615b5..1a8bd9b 100644 --- a/SDWebImageSwiftUI/Classes/AnimatedImage.swift +++ b/SDWebImageSwiftUI/Classes/AnimatedImage.swift @@ -10,17 +10,42 @@ import SwiftUI import SDWebImage public struct AnimatedImage: UIViewRepresentable { - public var url: URL? + var url: URL? + var name: String? + var bundle: Bundle? + var data: Data? + var scale: Length = 0 public init(url: URL?) { self.url = url } + public init(name: String?, bundle: Bundle? = nil) { + self.name = name + self.bundle = bundle + } + + public init(data: Data, scale: Length = 0) { + self.data = data + self.scale = scale + } + public func makeUIView(context: UIViewRepresentableContext) -> SDAnimatedImageView { SDAnimatedImageView() } public func updateUIView(_ uiView: SDAnimatedImageView, context: UIViewRepresentableContext) { - uiView.sd_setImage(with: url) + if let url = url { + uiView.sd_setImage(with: url) + return + } + if let name = name { + uiView.image = SDAnimatedImage(named: name, in: bundle, compatibleWith: nil) + return + } + if let data = data { + uiView.image = SDAnimatedImage(data: data, scale: scale) + return + } } }