Files
suixinkan_uikit/suixinkan/UI/CloudDrive/CloudDrivePreviewViewController.swift

76 lines
2.1 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// CloudDrivePreviewViewController.swift
// suixinkan
//
import AVKit
import Kingfisher
import SnapKit
import UIKit
///
final class CloudDrivePreviewViewController: BaseViewController {
private let file: CloudFile
private let imageView = UIImageView()
private var playerViewController: AVPlayerViewController?
///
init(file: CloudFile) {
self.file = file
super.init(nibName: nil, bundle: nil)
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func setupNavigationBar() {
title = ""
navigationController?.navigationBar.tintColor = .white
}
override func setupUI() {
view.backgroundColor = .black
if file.isVideo {
setupVideo()
} else {
setupImage()
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.navigationBar.tintColor = AppColor.primary
playerViewController?.player?.pause()
}
private func setupImage() {
imageView.contentMode = .scaleAspectFit
imageView.backgroundColor = .black
view.addSubview(imageView)
imageView.snp.makeConstraints { make in
make.edges.equalTo(view.safeAreaLayoutGuide)
}
if let url = URL(string: file.fileUrl) {
imageView.kf.setImage(with: url)
}
}
private func setupVideo() {
guard let url = URL(string: file.fileUrl) else { return }
let player = AVPlayer(url: url)
let controller = AVPlayerViewController()
controller.player = player
controller.view.backgroundColor = .black
addChild(controller)
view.addSubview(controller.view)
controller.view.snp.makeConstraints { make in
make.edges.equalTo(view.safeAreaLayoutGuide)
}
controller.didMove(toParent: self)
playerViewController = controller
player.play()
}
}