mastodon-ios/MastodonSDK/Sources/MastodonUI/SwiftUI/AnimatedImage.swift

56 lines
1.4 KiB
Swift
Raw Normal View History

2021-07-16 15:21:18 +02:00
//
// AnimatedImage.swift
//
//
// Created by MainasuK Cirno on 2021-7-16.
//
import SwiftUI
import Nuke
import FLAnimatedImage
2021-07-19 11:12:45 +02:00
public struct AnimatedImage: UIViewRepresentable {
2021-07-16 15:21:18 +02:00
2021-07-19 11:12:45 +02:00
public let imageURL: URL?
2021-07-16 15:21:18 +02:00
2021-07-19 11:12:45 +02:00
public init(imageURL: URL?) {
self.imageURL = imageURL
}
public func makeUIView(context: Context) -> FLAnimatedImageViewProxy {
2021-07-16 15:21:18 +02:00
let proxy = FLAnimatedImageViewProxy(frame: .zero)
Nuke.loadImage(with: imageURL, into: proxy.imageView)
return proxy
}
2021-07-19 11:12:45 +02:00
public func updateUIView(_ proxy: FLAnimatedImageViewProxy, context: Context) {
2021-07-16 15:21:18 +02:00
Nuke.cancelRequest(for: proxy.imageView)
Nuke.loadImage(with: imageURL, into: proxy.imageView)
}
}
2021-07-19 11:12:45 +02:00
final public class FLAnimatedImageViewProxy: UIView {
2021-07-16 15:21:18 +02:00
let imageView = FLAnimatedImageView()
override init(frame: CGRect) {
super.init(frame: frame)
imageView.translatesAutoresizingMaskIntoConstraints = false
addSubview(imageView)
imageView.pinToParent()
2021-07-16 15:21:18 +02:00
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
struct AnimatedImage_Previews: PreviewProvider {
static var previews: some View {
AnimatedImage(
imageURL: URL(string: "https://upload.wikimedia.org/wikipedia/commons/2/2c/Rotating_earth_%28large%29.gif")
)
.frame(width: 300, height: 300)
}
}