71 lines
2.0 KiB
Swift
71 lines
2.0 KiB
Swift
//
|
|
// ContextMenuImagePreviewViewController.swift
|
|
// Mastodon
|
|
//
|
|
// Created by MainasuK Cirno on 2021-4-30.
|
|
//
|
|
|
|
import func AVFoundation.AVMakeRect
|
|
import UIKit
|
|
import Combine
|
|
import Nuke
|
|
import FLAnimatedImage
|
|
|
|
final class ContextMenuImagePreviewViewController: UIViewController {
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
var viewModel: ContextMenuImagePreviewViewModel!
|
|
|
|
var imageTask: ImageTask?
|
|
let imageView: UIImageView = {
|
|
let imageView = FLAnimatedImageView()
|
|
imageView.contentMode = .scaleAspectFill
|
|
imageView.layer.masksToBounds = true
|
|
return imageView
|
|
}()
|
|
|
|
deinit {
|
|
imageTask?.cancel()
|
|
imageTask = nil
|
|
}
|
|
|
|
}
|
|
|
|
extension ContextMenuImagePreviewViewController {
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
imageView.translatesAutoresizingMaskIntoConstraints = false
|
|
view.addSubview(imageView)
|
|
NSLayoutConstraint.activate([
|
|
imageView.topAnchor.constraint(equalTo: view.topAnchor),
|
|
imageView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
imageView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
|
imageView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
|
|
])
|
|
|
|
imageView.image = viewModel.thumbnail
|
|
|
|
let frame = AVMakeRect(aspectRatio: viewModel.aspectRatio, insideRect: view.bounds)
|
|
preferredContentSize = frame.size
|
|
|
|
viewModel.url
|
|
.sink { [weak self] url in
|
|
guard let self = self else { return }
|
|
guard let url = url else { return }
|
|
self.imageTask = Nuke.loadImage(
|
|
with: url,
|
|
options: ImageLoadingOptions(
|
|
placeholder: self.viewModel.thumbnail,
|
|
transition: .fadeIn(duration: 0.2)
|
|
),
|
|
into: self.imageView
|
|
)
|
|
}
|
|
.store(in: &disposeBag)
|
|
}
|
|
|
|
}
|