AutoSizingTableViewController.swift 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // AutoSizingTableViewController.swift
  3. // Kingfisher
  4. //
  5. // Created by JP20028 on 2021/03/15.
  6. //
  7. // Copyright (c) 2021 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. import UIKit
  27. import Kingfisher
  28. // Cell with an image view (loading by Kingfisher) with fix width and dynamic height which keeps the image with aspect ratio.
  29. class AutoSizingTableViewCell: UITableViewCell {
  30. static let p = ResizingImageProcessor(referenceSize: .init(width: 200, height: CGFloat.infinity), mode: .aspectFit)
  31. @IBOutlet weak var leadingImageView: UIImageView!
  32. @IBOutlet weak var sizeLabel: UILabel!
  33. var updateLayout: (() -> Void)?
  34. func set(with url: URL) {
  35. leadingImageView.kf.setImage(with: url, options: [.processor(AutoSizingTableViewCell.p), .transition(.fade(1))]) { r in
  36. if case .success(let value) = r {
  37. self.sizeLabel.text = "\(value.image.size.width) x \(value.image.size.height)"
  38. self.updateLayout?()
  39. } else {
  40. self.sizeLabel.text = ""
  41. }
  42. }
  43. }
  44. }
  45. class AutoSizingTableViewController: UIViewController {
  46. @IBOutlet weak var tableView: UITableView!
  47. var data: [Int] = Array(1..<700)
  48. override func viewDidLoad() {
  49. super.viewDidLoad()
  50. tableView.estimatedRowHeight = 150
  51. }
  52. override func viewDidAppear(_ animated: Bool) {
  53. super.viewDidAppear(animated)
  54. UIView.setAnimationsEnabled(false)
  55. }
  56. override func viewDidDisappear(_ animated: Bool) {
  57. UIView.setAnimationsEnabled(true)
  58. super.viewDidDisappear(animated)
  59. }
  60. }
  61. extension AutoSizingTableViewController: UITableViewDataSource {
  62. private func updateLayout() {
  63. tableView.beginUpdates()
  64. tableView.endUpdates()
  65. }
  66. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  67. return data.count
  68. }
  69. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  70. let cell = tableView.dequeueReusableCell(withIdentifier: "AutoSizingTableViewCell", for: indexPath) as! AutoSizingTableViewCell
  71. cell.set(with: ImageLoader.roseImage(index: data[indexPath.row]))
  72. cell.updateLayout = { [weak self] in
  73. self?.updateLayout()
  74. }
  75. return cell
  76. }
  77. }