LoadingViewController.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. ////
  2. //// LoadingViewController.swift
  3. //// QuickSearchLocation
  4. ////
  5. //// Created by Destiny on 2024/12/31.
  6. ////
  7. //
  8. //import UIKit
  9. //import Photos
  10. //
  11. //class LoadingViewController: UIViewController {
  12. // private let photoClassifier = PhotoClassifier()
  13. // private let progressView = UIProgressView(progressViewStyle: .default)
  14. // private let statusLabel = UILabel()
  15. // private let resultLabel = UILabel()
  16. // private let collectionView: UICollectionView
  17. // private var similarGroups: [[PHAsset]] = []
  18. // private var timer: Timer?
  19. //
  20. // // 添加详细进度标签
  21. // private let detailProgressLabel = UILabel()
  22. // private let percentageLabel = UILabel()
  23. //
  24. // override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
  25. // // 初始化 CollectionView
  26. // let layout = UICollectionViewFlowLayout()
  27. // layout.scrollDirection = .vertical
  28. // layout.minimumLineSpacing = 10
  29. // layout.minimumInteritemSpacing = 5
  30. //
  31. // let screenWidth = UIScreen.main.bounds.width
  32. // let width = (screenWidth - 20) / 3
  33. // layout.itemSize = CGSize(width: width, height: width)
  34. // layout.sectionInset = UIEdgeInsets(top: 10, left: 5, bottom: 10, right: 5)
  35. // layout.headerReferenceSize = CGSize(width: screenWidth, height: 40)
  36. //
  37. // collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
  38. //
  39. // super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
  40. // }
  41. //
  42. // required init?(coder: NSCoder) {
  43. // fatalError("init(coder:) has not been implemented")
  44. // }
  45. //
  46. // override func viewDidLoad() {
  47. // super.viewDidLoad()
  48. // setupUI()
  49. // startClassifying()
  50. // }
  51. //
  52. // private func setupUI() {
  53. // view.backgroundColor = .white
  54. //
  55. // // 设置进度容器视图
  56. // let progressContainer = UIView()
  57. // progressContainer.translatesAutoresizingMaskIntoConstraints = false
  58. // view.addSubview(progressContainer)
  59. //
  60. // // 配置所有标签
  61. // statusLabel.textAlignment = .center
  62. // statusLabel.font = .systemFont(ofSize: 16, weight: .medium)
  63. //
  64. // detailProgressLabel.textAlignment = .center
  65. // detailProgressLabel.font = .systemFont(ofSize: 14)
  66. // detailProgressLabel.textColor = .darkGray
  67. //
  68. // percentageLabel.textAlignment = .center
  69. // percentageLabel.font = .systemFont(ofSize: 14, weight: .medium)
  70. // percentageLabel.textColor = .systemBlue
  71. //
  72. // resultLabel.textAlignment = .center
  73. // resultLabel.numberOfLines = 0
  74. // resultLabel.font = .systemFont(ofSize: 14)
  75. //
  76. // // 配置进度条
  77. // progressView.progressTintColor = .systemBlue
  78. // progressView.trackTintColor = .systemGray5
  79. //
  80. // // 添加所有视图
  81. // [statusLabel, progressView, detailProgressLabel, percentageLabel, resultLabel].forEach {
  82. // $0.translatesAutoresizingMaskIntoConstraints = false
  83. // progressContainer.addSubview($0)
  84. // }
  85. //
  86. // // 布局约束
  87. // NSLayoutConstraint.activate([
  88. // progressContainer.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
  89. // progressContainer.leadingAnchor.constraint(equalTo: view.leadingAnchor),
  90. // progressContainer.trailingAnchor.constraint(equalTo: view.trailingAnchor),
  91. // progressContainer.heightAnchor.constraint(equalToConstant: 180),
  92. //
  93. // statusLabel.topAnchor.constraint(equalTo: progressContainer.topAnchor, constant: 20),
  94. // statusLabel.centerXAnchor.constraint(equalTo: progressContainer.centerXAnchor),
  95. //
  96. // progressView.topAnchor.constraint(equalTo: statusLabel.bottomAnchor, constant: 15),
  97. // progressView.centerXAnchor.constraint(equalTo: progressContainer.centerXAnchor),
  98. // progressView.widthAnchor.constraint(equalTo: progressContainer.widthAnchor, multiplier: 0.8),
  99. //
  100. // detailProgressLabel.topAnchor.constraint(equalTo: progressView.bottomAnchor, constant: 10),
  101. // detailProgressLabel.centerXAnchor.constraint(equalTo: progressContainer.centerXAnchor),
  102. //
  103. // percentageLabel.topAnchor.constraint(equalTo: detailProgressLabel.bottomAnchor, constant: 5),
  104. // percentageLabel.centerXAnchor.constraint(equalTo: progressContainer.centerXAnchor),
  105. //
  106. // resultLabel.topAnchor.constraint(equalTo: percentageLabel.bottomAnchor, constant: 15),
  107. // resultLabel.centerXAnchor.constraint(equalTo: progressContainer.centerXAnchor),
  108. // resultLabel.widthAnchor.constraint(equalTo: progressContainer.widthAnchor, multiplier: 0.8)
  109. // ])
  110. //
  111. // // 设置 CollectionView
  112. // collectionView.backgroundColor = .white
  113. // collectionView.delegate = self
  114. // collectionView.dataSource = self
  115. // collectionView.register(PhotoCell.self, forCellWithReuseIdentifier: "PhotoCell")
  116. // collectionView.register(
  117. // HeaderView.self,
  118. // forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader,
  119. // withReuseIdentifier: "Header"
  120. // )
  121. //
  122. // collectionView.translatesAutoresizingMaskIntoConstraints = false
  123. // view.addSubview(collectionView)
  124. //
  125. // NSLayoutConstraint.activate([
  126. // collectionView.topAnchor.constraint(equalTo: progressContainer.bottomAnchor),
  127. // collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
  128. // collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
  129. // collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
  130. // ])
  131. // }
  132. //
  133. // private func startClassifying() {
  134. // // 重置UI
  135. // progressView.progress = 0
  136. // statusLabel.text = "正在准备..."
  137. // detailProgressLabel.text = "初始化中"
  138. // percentageLabel.text = "0%"
  139. // resultLabel.text = ""
  140. //
  141. // // 请求相册权限
  142. // PHPhotoLibrary.requestAuthorization { [weak self] status in
  143. // guard let self = self else { return }
  144. //
  145. // DispatchQueue.main.async {
  146. // if status == .authorized {
  147. // self.beginPhotoClassification()
  148. // } else {
  149. // self.statusLabel.text = "需要相册访问权限"
  150. // self.detailProgressLabel.text = "请在设置中允许访问相册"
  151. // }
  152. // }
  153. // }
  154. // }
  155. //
  156. // private func beginPhotoClassification() {
  157. // statusLabel.text = "正在加载照片..."
  158. //
  159. // let fetchOptions = PHFetchOptions()
  160. // let allPhotos = PHAsset.fetchAssets(with: .image, options: fetchOptions)
  161. //
  162. // // 更新总数
  163. // detailProgressLabel.text = "共发现 \(allPhotos.count) 张照片"
  164. //
  165. // photoClassifier.classifyPhotos(
  166. // assets: allPhotos,
  167. // progressHandler: { [weak self] (stage, progress) in
  168. // DispatchQueue.main.async {
  169. // self?.updateProgress(stage: stage, progress: progress)
  170. // }
  171. // },
  172. // completion: { [weak self] result in
  173. // guard let self = self else { return }
  174. //
  175. // DispatchQueue.main.async {
  176. // self.updateProgress(stage: "分类完成", progress: 1.0)
  177. //
  178. // // 更新结果显示
  179. // var resultText = "分类结果:\n"
  180. // resultText += "截图:\(result.screenshots.count) 张\n"
  181. // resultText += "相似照片组:\(result.similarPhotos.count) 组"
  182. // self.resultLabel.text = resultText
  183. //
  184. // // 更新相似照片展示
  185. // self.similarGroups = result.similarPhotos
  186. // self.collectionView.reloadData()
  187. // }
  188. // }
  189. // )
  190. // }
  191. //
  192. // private func updateProgress(stage: String, progress: Float) {
  193. // statusLabel.text = stage
  194. // progressView.progress = progress
  195. // percentageLabel.text = "\(Int(progress * 100))%"
  196. //
  197. // switch stage {
  198. // case "正在加载照片...":
  199. // detailProgressLabel.text = "正在读取照片数据"
  200. // case "正在提取特征...":
  201. // detailProgressLabel.text = "正在分析照片内容"
  202. // case "正在比较相似度...":
  203. // detailProgressLabel.text = "正在查找相似照片"
  204. // case "分类完成":
  205. // detailProgressLabel.text = "处理完成"
  206. // default:
  207. // break
  208. // }
  209. // }
  210. //}
  211. //
  212. //// MARK: - UICollectionView DataSource & Delegate
  213. //extension LoadingViewController: UICollectionViewDataSource, UICollectionViewDelegate {
  214. // func numberOfSections(in collectionView: UICollectionView) -> Int {
  215. // return similarGroups.count
  216. // }
  217. //
  218. // func collectionView(_ collectionView: UICollectionView,
  219. // numberOfItemsInSection section: Int) -> Int {
  220. // return similarGroups[section].count
  221. // }
  222. //
  223. // func collectionView(_ collectionView: UICollectionView,
  224. // cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  225. // let cell = collectionView.dequeueReusableCell(
  226. // withReuseIdentifier: "PhotoCell",
  227. // for: indexPath
  228. // ) as! PhotoCell
  229. //
  230. // let asset = similarGroups[indexPath.section][indexPath.item]
  231. // cell.configure(with: asset)
  232. // return cell
  233. // }
  234. //
  235. // func collectionView(_ collectionView: UICollectionView,
  236. // viewForSupplementaryElementOfKind kind: String,
  237. // at indexPath: IndexPath) -> UICollectionReusableView {
  238. // if kind == UICollectionView.elementKindSectionHeader {
  239. // let header = collectionView.dequeueReusableSupplementaryView(
  240. // ofKind: kind,
  241. // withReuseIdentifier: "Header",
  242. // for: indexPath
  243. // ) as! HeaderView
  244. //
  245. // let groupCount = similarGroups[indexPath.section].count
  246. // header.titleLabel.text = "相似组 \(indexPath.section + 1) (\(groupCount) 张)"
  247. // return header
  248. // }
  249. // return UICollectionReusableView()
  250. // }
  251. //}
  252. //
  253. //// MARK: - PhotoCell
  254. //class PhotoCell: UICollectionViewCell {
  255. // private let imageView = UIImageView()
  256. //
  257. // override init(frame: CGRect) {
  258. // super.init(frame: frame)
  259. // setupUI()
  260. // }
  261. //
  262. // required init?(coder: NSCoder) {
  263. // fatalError("init(coder:) has not been implemented")
  264. // }
  265. //
  266. // private func setupUI() {
  267. // imageView.contentMode = .scaleAspectFill
  268. // imageView.clipsToBounds = true
  269. // imageView.translatesAutoresizingMaskIntoConstraints = false
  270. // contentView.addSubview(imageView)
  271. //
  272. // NSLayoutConstraint.activate([
  273. // imageView.topAnchor.constraint(equalTo: contentView.topAnchor),
  274. // imageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
  275. // imageView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
  276. // imageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
  277. // ])
  278. // }
  279. //
  280. // func configure(with asset: PHAsset) {
  281. // let options = PHImageRequestOptions()
  282. // options.deliveryMode = .fastFormat
  283. //
  284. // PHImageManager.default().requestImage(
  285. // for: asset,
  286. // targetSize: CGSize(width: 200, height: 200),
  287. // contentMode: .aspectFill,
  288. // options: options
  289. // ) { [weak self] image, _ in
  290. // self?.imageView.image = image
  291. // }
  292. // }
  293. //}
  294. //
  295. //// MARK: - HeaderView
  296. //class HeaderView: UICollectionReusableView {
  297. // let titleLabel = UILabel()
  298. //
  299. // override init(frame: CGRect) {
  300. // super.init(frame: frame)
  301. // setupUI()
  302. // }
  303. //
  304. // required init?(coder: NSCoder) {
  305. // fatalError("init(coder:) has not been implemented")
  306. // }
  307. //
  308. // private func setupUI() {
  309. // titleLabel.font = .systemFont(ofSize: 16, weight: .medium)
  310. // titleLabel.translatesAutoresizingMaskIntoConstraints = false
  311. // addSubview(titleLabel)
  312. //
  313. // NSLayoutConstraint.activate([
  314. // titleLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 15),
  315. // titleLabel.centerYAnchor.constraint(equalTo: centerYAnchor)
  316. // ])
  317. // }
  318. //}